lua-users home
lua-l archive

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


In C++ any reserved 'word' (are +, -, * etc words?) can be overridden. In ruby they can be overridden as well (despite being told not to ;) as I created an and method in a class and executed the method fine.

The ability to override the basic operations (keywords) supported by a language is one of the keys to true OOP.  Most of these things are handled in lua as metatable overrides, maybe we just need more metatable entries for the other operations? If that were the case a userdata or custom table 'type' could use the built in and, or, not etc keywords and do the right thing based on the argument types...

Hrm, if one actually implemented this, it would make the language definable in terms of itself...  Now thats crazy!

I'm honestly not sure what the optimal solution would be. Lua was not designed as a full OO language (nothing wrong with that!), so from a purists point of view there is no room for this kind of thing.  On the other hand allowing such constructs won't break anything (well at least the table index constructs) so why not allow them?

Mike

On Wed, Jan 7, 2009 at 10:53 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> Hmm...  I wonder how hard it would be to change the parser so that when a
> table is being indexed with . or : it acts similar to other languages (such
> as ruby) and interprets everything after the . or : up to the first bit of
> white space explicitly as a name.  In ruby (and probably python)
> something.and is legal.

According to "Programming Ruby", "and" (and "or", "not", etc.) should
not be used as a method name (2nd edition, p. 328).

A small Python program:

a.x = 4;
a.or = 3;

Output:

 File "temp", line 2
   a.or = 3;
      ^
SyntaxError: invalid syntax

And the question is not whether it is easy or hard to change the parser
to allow such names. The question is whether this would be a good
idea.  The concept of reserved words has a long tradition in programming
languages, being adopted by most current languages (C, C++, Java, C#,
Python?, Haskell, Erlang, Pascal, etc.)

(BTW, in Erlang both band and bor are reserved ;).

-- Roberto