lua-users home
lua-l archive

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


Grellier, Thierry wrote:
Oh well, if it is time to add some suggestions.

Just a lexer one : adding a `const´ keyword
I know people are not really happy with adding endlessly some keywords, and that may breaks some programs already using it as a variable name... But I think this is really helpful to auto document code.
I don't intend the interpreter to perform anything with it, but just as `;´ has been added to be immediately forgotten, this could be nice to add this to help documenting the code.
It can then be a special kind of comment discarded by lexer so that code overhead is very limited... Or be more tightly coupled to grammar to limit risks of ambiguity with variable names...

namelist ::= Name [`const´] {`,´ Name [`const´]}
var ::= Name [`const´] | prefixexp `[´ exp `]´ | prefixexp `.´ Name funcbody ::= `(´ [parlist1] `)´ [`const´] block `end´

If it really is just documentation, then why not just use a comment?

function foo(x --const
   , y --const
) --const


Then there's the issue of what it means. If 'x' is const does that mean that the variable x is not modifed, or does it mean that nothing referred to by x is modified. If x references a table, does it mean that the table contents don't change? Does it mean that the variable x won't be used to change the table contents; can the table be changed via a different variable that happens to refer to the same table (aliased).

Most of these different sorts of const are useful concepts, but using one keyword to try and cover them will be confusing.

My experience from other languages (C, C++, Objective C, Java, Smalltalk, etc) suggests that trying to add some notion of const to the language is detrimental. It's rarely of use to the compiler, adds little or no safety barrier for programmers, is confusing, and isn't always helpful documentation. I've seen grown men weep over C's const, and highly paid C++ professionals argue for hours over the meaning of const in C++.

The right place to annotate things with notions of constantness is the type hierarchy. See, for example, java.awt.Raster / java.awt.WritableRaster (from Java's 2D api), NSDictionary / NSMutableDictionary (from Objective-C's foundation classes).

drj