lua-users home
lua-l archive

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


On Wed, Jan 7, 2009 at 8:36 AM, Evan DeMond <evan.demond@gmail.com> wrote:
> On Wed, Jan 7, 2009 at 11:28 AM, Mike Panetta <panetta.mike@gmail.com>
> wrote:
>>
>> In C++ any reserved 'word' (are +, -, * etc words?) can be overridden.
>> [snip]
>
> I'm pretty sure this isn't true. Don't confuse this with C++ operator
> overloading - the language provides a specific set of operators, not just
> any reserved word, which can be overloaded. You can't use C++'s keywords as
> identifiers, for instance. Try to declare a variable called "return"...

Or "if", or "class", ...

>> The ability to override the basic operations (keywords) supported by a
>> language is one of the keys to true OOP.
>
> I don't really follow this point. Not encapsulation, inheritance, and
> polymorphism?

Only on a lua list could the OP call python, C++, and Java "not true
OOP" without starting a flame war. :-)

Keywords aren't overrideable in many languages, python included.

>>> class Fu:
...  def hi(self): print("hi")
...  def return(self): print("return")
  File "<stdin>", line 3
    def return(self): print("return")
             ^
SyntaxError: invalid syntax

Ruby doesn't have keywords in the traditional sense, it sends named
messages, like its Smalltalk ancestor. You can use the builtin names
for your own methods:

irb(main):001:0> def if() print"hi" end
=> nil
irb(main):002:0> self.if()
hi=> nil

But you can't "override" them in the sense of changing their meaning:

irb(main):004:0> def Kernel.if(a) print("got "+ + a.to_s) end
=> nil
irb(main):005:0> if nil then print"four" end
=> nil


Anyhow, Lua is pretty typical in having reserved words, it is Ruby that is not.


Btw, the ability to use any valid token as the name of a table key is
quite useful when metaprogramming, so I actually think it would be an
improvement in lua 5.2 to allows

obj:and"three"

and

tab.and = "AND"

and

tab = { and ="<AND>", or = "<OR>" }

Its would be confusing to use and as local variables, and I wouldn't
change the names of bitlib's functions, but it would be handy for
autogenerated code, for example, not to have to mangle names into the
namespace.

Of course, if you know you are going to be dealing with reserved
words, you can change your lookup style to not use the "." syntax
sugar, and use explicit [] instead:

obj["and"]("three")

and

tab = { ["and"] = "<AND>" }

Cheers,
Sam