Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit) (+1)

Interfaces are only "aware of" direct attribute reads and writes.

# read:
field1.data
# write
field1.data:expr

When you read field1.data, the result is an immutable value- in your example a list of lists. Deeper indexing can "amend" that value, but it's no longer connected to the interface. Conceptually these lines are all equivalent:

field1.data[0][1]:5
(field1.data)[0][1]:5
((list 1,2),(list 3,4))[0][1]:5

An indexed amendment produces a modified copy of the original immutable value. If the root of the expression is a variable, its contents is updated. In some cases this may look like a mutation, but anything that referred to the original value will still see the unmodified copy:

foo:11,22,33
bar:foo
bar[1]:99

foo # (11,22,33)
bar # (11,99,33)

Does that help clarify?

(+1)

Ah, I think I see.

  • In the ’mydict.data[0][1]:5case, Lil traces from the originalmydict` variable down into the nested list, and so after creating an updated sublist, it can trace all the way back up to the original variable and update that too.
  • In the field1.data[0][1]:5 case, field1.data produces a data structure out of (from Lil’s point of view) thin air, and so Lil doesn’t know how to update it.

Is that right?

If the root of the expression is a variable, its contents is updated. In some cases this may look like a mutation, but anything that referred to the original value will still see the unmodified copy…

I originally tried to illustrate Lil’s immutable, functional nature by modifying a nested value and showing the original variable still had its original value, but… Lil was too smart for me, and the variable was updated to point at the modified value. I also considered using the “assign A to B, modify A, B is unchanged” example, but that just looks like a copy of the value is made immediately, rather than being copy-on-write.

I am still amused by the irony that interfaces make a better illustration of Lil’s immutable values, for the wrong reason. :)

Correct.

You can break the connection between a variable and the "strand" of an indexing expression by wrapping part of the prefix in parentheses, forcing it to be evaluated first, separately from the following parts of the strand. Consider these cases:

a:11,22,33
b:a
# these are 'simple' assignments;
# 'a' and 'b' both refer to (11,99,33)

a[1]:99
# evaluates to (11,99,33),
# 'a' now contains (11,99,33)
# 'b' still contains (11,22,33)

(b)[1]:88
# evaluates to (11,88,33),
# but the contents of 'b' is unchanged.
# equivalent to (11,22,33)[1]:88

And this wouldn't be syntactically valid at all, since it has neither a direct variable name at the "head" of the strand nor any indices at which to amend:

(b[1]):77

But we can do weird-looking things like this, amending the empty list at an index to create a singleton dictionary:

().foo:"bar"
# evaluates to {"foo":"bar"}
# equivalent to ()["foo"]:"bar"