Experimental new feature
|
To read the contents of a file in Lua you might use a function Lua 5.4 alpha has an experimental feature, local variables with attributes and the to-close attribute, so that you can instead write this more snappily as: A to-close local variable triggers a close event when it goes out of scope. For file handles this is just closing the file, but in general the programmer can set the procedure for this event by setting a value for the key __close in an object’s metatable.Another attribute is const . which makes a local variable readonly. It raises an error if it is later assigned a value. The interpreter can do certain optimizations if it knows that a value is constant.
I have not yet got down to trying any of this out in RISC OS, but I thought the to-close attribute was worth a mention. |
|
Sounds like an interesting optimisation.
This, on the other hand, I find astonishing that it took until version 5.4 before the idea of constants was introduced. |
|
If my memory is not playing tricks, Lua 3 had macros which could be used for defining constants. But then it was decided that macros made for too much confusion, so they were pulled. You can define readonly values in Lua by using metamethods, but that is a bit like flying from London to Paris via Rio. Lua has also long had a certain amount of partial evaluation, so that arithmetic expressions without variables are evaluated immediately during the compilation to bytecode. |