lua-users home
lua-l archive

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


Am 23.03.2010 19:27, schrieb David Given:
Unless it becomes utterly trivial --- that is, the const-ness applies to
the variable only, and not the contents --- you very quickly start
straying down the path towards a full type system...
I'd like to say, the const-ness at least applies to to type-nees of the variable :-)
And also to the contents, if the value itself is immutable. So:

final x=true will always be of type boolean, and will always be true (true is immutable) final y="YES" will always be a string and will always be the same string (strings are immutable [PiL 2.4] ) final t={} will always be a table (and will always be the same table (!) ), but the table itself is mutable, so t.x=1 is fine, even t[#t+1]="x" is perfect legal. final fd=io.open("text.txt","w") will always be a userdata (and always the same userdata), but the underlying c-data (in this case the file-state) may be altered with
fd:write(...) or fd:close()

Conclusions:
final x=false if x then something() end -- eligible for optimization
final t="YES" if t=="no" then something() end -- eligible for optimization
final y={} if y then something() end -- too! (y will never be nil or false)
final z={} if #z>0 then something() end -- not (because the table is mutable so #z can vary)

I dont know, how complicated this would be to implement (as a pure lua-application-writer I did not look into luac yet), but this looks promising for me.

Regards Jørgen