lua-users home
lua-l archive

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


The keyword 'final' is a specialization of the keyword 'local'.
The goal is the defensive programming.
There are 2 new grammar rules :
    stat -> FINAL FUNCTION NAME body
    stat -> FINAL NAME {`,' NAME} `=' explist1
Only the parser is modified and produces new errors.
The VM is not modified (The goal is not the performance optimization).
This proposal has not equivalent in http://lua-users.org/wiki/ImmutableObjects.

final a = 2
a = 3           --> error : assign a final variable

final c         --> error : '=' expected near 'final'

final t = {}
t.a = 1         --> ok
t = {}          --> error : assign a final variable

The first use cases are internal :
1) currently, the effect of an assignment of a 'for' control variable
is unpredictable (see PiL). These control variables could marked as final.
2) the implicit argument 'self' of a method could be marked as final.

This new keyword could easily be back ported to old version as a simple alias
of the keyword 'local'.

François Perrad

Note: all attached patches are based on Lua 5.1.4

Attachment: 0001-add-keyword-final.patch
Description: Binary data

Attachment: 0002-for-variables.patch
Description: Binary data

Attachment: 0003-self.patch
Description: Binary data

Attachment: 0001-alias-final-local.patch
Description: Binary data