A "to decide what number is" question

I wasn’t 100% sure that this would work, but I hoped it would. It doesn’t work, but it doesn’t give an error message either:

The orchard is a room.

A weight is a kind of value. 10lb specifies a weight. Everything has a weight. A thing usually has weight 2lb. 

A basket is a container in the orchard. 

An apple is a kind of thing. 10 apples are in the basket.

To decide what weight is the weight of the basket:
	let Foo be 2lb;
	if the basket contains something:
		repeat with item running through things in the basket:
			let Foo be Foo plus the weight of the item;
	decide on Foo.
		
Every turn:
	say "The weight of the basket is [the weight of the basket]."

The idea is that the weight of the basket plus contents would be calculated on the fly by the above “to decide what weight” rule each time it’s referred to, but the every turn rule gives the weight as 2lb. Can anyone shed light on why it doesn’t work?

I think Inform is confused because it understands “the weight of the basket” as the weight property of the basket, rather than the identically named to decide phrase you created. The code seems to work for me when I change it to “to decide what weight is the basket-weight” (and update the corresponding say command at the end).

But it was the weight property of the basket that I was attempting to set with the decide phrase. I don’t want to create a separate variable.

I take it then that you can’t use a decide phrase to set an existing variable or property, and that’s fine, but in which case I would expect Inform to produce an error…

Sorry, maybe I’m misunderstanding - wouldn’t that lead to a lot of wonkiness? Like on turn 1, the weight of the basket is two pounds (representing the weight of the wicker etc.), and it’s carrying 20 pounds of apples. If you say ok, now the weight property of the basket is 22 pounds, the next turn the calculation will get you to 42 pounds if the apples are still there, then 62 the turn after that… wouldn’t it be better to keep separate track of the weight of the basket plus its contents?

No, because (unless I’m misunderstanding something!) the weight of the basket would be recalculated every time the value is referred to and it always starts with two pounds (the weight of the wicker), and adds the combined weights of the contents on that turn. It wouldn’t be cumulative, so far as I can see.

@DeusIrae what you suggested works perfectly, but it requires the addition of an every turn rule to change the weight of the basket to the variable basket-weight, which is what I wanted to avoid:

The orchard is a room.

A weight is a kind of value. 10lb specifies a weight. Everything has a weight. A thing usually has weight 2lb. 

A basket is a container in the orchard. 

An apple is a kind of thing. 10 apples are in the basket.

To decide what weight is the basket-weight:
	let Foo be 2lb;
	if the basket contains something:
		repeat with item running through things in the basket:
			let Foo be Foo plus the weight of the item;
	decide on Foo.
		
Every turn:
	now the weight of the basket is basket-weight;
	say "The weight of the basket is [the weight of the basket]."
1 Like

Ah, OK, I think I maybe get the confusion? You’ve set up weight as a property that everything has, and properties are “durable” – they persist across rules, turns, etc. But to decide phrases (I think!) just return the result of a temporary calculation and don’t intrinsically do anything to store that result anywhere. So that’s the conceptual mismatch, and then the syntactical issue is just that your “to decide” phrase never actually runs, because “the weight of the basket” is something that the compiler thinks it already knows – that’s just the weight property of the basket object, two pounds.

I think you can get around this by saying “The basket has a weight called the basket-weight” and then run the to decide phrase on that – does that get you the behavior you want?

EDIT: and d’oh, I just realized why I was wrong on the escalating-weight point – you smartly had Foo start at 2 pounds, not the actual weight of the basket property! Sorry for missing that and muddling the waters still further.

1 Like

What you’re going for here is a traditional Object-oriented Programming method, which can simply return a property value or be overridden to return a calculated value. And Inform doesn’t have those. You could do something like…

To decide what weight is the total weight of (C - a container):
	let Foo be the weight of C;
	repeat with item running through things in C:
		let Foo be Foo plus the weight of the item;
	decide on Foo.
		
Every turn:
	say "The weight of the basket is [the total weight of the basket]."
2 Likes

Yes, we cross-posted on this, but you are right about a conceptual mismatch. I think I hadn’t really wrapped my head around how to decide phrases work, but this discussion is helping to clarify things for me, thank you!

1 Like

Cool! So I can’t really avoid that every turn rule. Oh well! Thanks to both of you for helping to clear that up for me!

1 Like

or, generalized:

To decide what weight is the total weight of (T - a thing):
  let result be the weight of T;
  repeat with item running through things held by T begin;
    now result is result + the total weight of item;
  end repeat;
  decide on result;
3 Likes

WWI 15.17 Totals has something to say that might be of use to you, allowing you to simplify and genericize the definition of the phrase:

To decide what weight is the laden weight of (T - thing): [see WWI 15.17 Totals]
    decide on total weight of things enclosed by T plus weight of T.

The total of... phrase is one of the many built-in conveniences for frequently-occurring tasks.

FYI, a phrase can be used in much the same way that a property could be, so I’m not sure that you absolutely need the every turn rule. Every use of laden weight of... will prompt a calculation when the value is needed.

3 Likes

You can avoid the every-turn rule by separating the base weight of an object (a settable property) from the total weight (a to-decide phrase that computes the total on the fly).

This is what Zed is describing.

2 Likes

This is perfect, thanks Otis! I don’t know how I missed that chapter in the docs.

Nice, Otis! I didn’t recall that in the slightest and clearly should reread Writing with Inform when the v10 version is here.

2 Likes