Computed new value properties

WI 4.8 talks about how properties can be defined with values attached. I’d like to write the equivalent of a “computed” property (or a property getter) for objects of the thing kind, in an attempt to enable JSON serialization.

Something to the effect of the below (this is an attempt, I know it’s not valid I7)

A thing has some text called the JSON representation.
The JSON representation of a thing is "{ 'name' : '[a thing]' }"

(obviously this would be extended to include other properties)

The issue with this approach is that I fear that this would result in each thing object having a property stored in memory that stores this value continuously for every object. I’d rather compute this on the fly when needed.

What’s the best way to do this?

For context, this is related to the project I mentioned in this thread:

Would something like this work?

to decide on what a thing is (thing - T) as JSON:
    decide on "{ 'name' : '[a T]' }";

say the coffee pot as JSON

Or perhaps

to say JSON for (thingy - a thing):
    say "{ 'name' : '[a thingy]' }";

say "[JSON for the coffee pot]"

Yes, that would work.

Depending on how you plan to interface with the story and how you want to process the text, you could also try to grab the output of the built-in TREE and SHOWME commands.

Maybe you’ll find my post on a somewhat related topic helpful:

1 Like

This thread is super helpful, thanks! I probably won’t try to capture the output of TREE/SHOWME but might dive into their source code to build what I’m going for.

Especially as examples for constructs to loop over and inspect the entire global state, I bet their source will be quite helpful.

I’m afraid you’re not going to be able to do this in the general case, i.e., you won’t be able to write an extension that one could just include that could automatically handle all the arbitrary kinds of things that a given story might have defined. With custom coding on a per story basis you should be able to.

See discussion at: Programatically printing all properties associated with object?. There’s a lot of introspection one might want that you can’t get.

I think it would likely be possible to do it by implementing another step between ni (the I7 compiler) and the I6 compiler that analyzed auto.inf and then generated more Inform 6 code that it would add to auto.inf. I’ve toyed with the idea. But it seems like a bad investment of effort given how unclear it is whether one would even finish such a thing before the next release of I7 not just broke it but utterly obliterated it.