[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Psuedo-proposal: const expression
- From: Robert Virding <robert.virding@...>
- Date: Fri, 09 Mar 2012 11:16:14 -0000 (GMT)
Coming from an (immutable) functional world I find this discussion of adding immutable data to Lua very interesting. Seeing that it exist together with mutable data it is very important to be very clear of what it means. So these as someone (Alex?) has already pointed out these two are different:
const t1 = {'a','b')
t2 = const ('a','b'}
The first is a t1 which always points to the same table, which it self is mutable, while the second is t2, which may be changed, pointing to a table which is immutable. My own personal opinion would be that the second is more useful. You could of course also have:
const t3 = const {'a','b'}
Declaring a table as immutable should not mean that all the tables it refers to automatically become immutable. That would be very confusing and imply that data can change under your feet in strange ways. Which is one reason for having immutable data.
I think you need to be very clear about WHY you would like to introduce immutability. Is it just to help the compiler do optimisation? You need to realise that it profoundly changes the way you write programs, it is not just a cute feature. So with t2 above, referring to an immutable table, then in a functional, immutable world doing
t2[#t2+1]='x' would result in creating a NEW table with contains all the old elements plus a new one. The old table remains unchanged; it is immutable. You would end up writing things like
t2p = t2[#t2+1]='x'
which gives you references to both the old and new tables. Immutable data will/should also change the meaning of equality. If I have
x1 = const {'a','b'}
x2 = const {'a','b'}
should x1 == x2? In an immutable world they should be. So immutable tables are not just equal if they are the same object but also if they have the fileds.
Coming from a functional world I find this natural and self-evident. There are a lot of benefits in having immutable data, it is a Big Win (tm).
Luerl does not at the moment have immutable tables as I have worked hard to make it Lua compatible.
Robert
(as the great Cato said "optional syntax delenda est")
----- Original Message -----
> > And then I feel, but can't back it up with anything, that
> > indirectly
> > introducing const in lua (for tables), begs for introduction of
> > const for
> > strings, or more of the opposite - mutable strings :)
> >
> > And I love Lua for it's constant strings.
>
> So you are shooting it down, for something else that has not been
> proposed?
>
> http://en.wikipedia.org/wiki/Straw_man
>
>