lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mar 9, 2012, at 8:57 , Axel Kittenberger wrote:
> As we like to talk about syntax so much :-) I would suggest the unary
> '*' as freeze operator.
> Freeze operator: * [expression]
> If the expression is a mutable table it returns a copy with the
> immutable flag set.
> If the expression is an immutable table already it has no effect.
> If the expression is any other primitive, it has no effect.
> 
> local foo = *bar  -- foo is now an immutabe copy of bar
> local foo = *{1, 2, 3, 4}  -- foo is immutable version of 1,2,3,4. foo
> itself can still be made to point to something else
> lock foo = *{1, 2, 3, 4} -- foo is now immutable and locked, #foo will
> always and ever be 4 in that case (unless another local foo overrides
> its scope). foo[1] will always and ever be 1.
> lock foo = {1,2,3,4} -- foo will always point to this table (saving a
> look up in use), but it contents can be changed
> 
> In case of local foo = *{1,2, 3, 4} too create a new table with a
> change value would look like this:
> 
> foo = *copy(t)[5] = 5 -- copy is an obvious simple function.

One problem with that syntax is that if we want the `func {}` syntax to work with frozen tables, we would need to express that as `func *{}`…which can't be told apart from multiplying func by a (mutable) empty table. I do like the idea of a sigil though. Currently out of the US keyboard, we aren't using !, @, $, ^, &, |, ?, and ~ as binary operators. Unfortunately I don't really like the aesthetics of any of them.

Thanks,
Matthew Frazier
http://leafstorm.us/