lua-users home
lua-l archive

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

And, again, in-end doesn't quite fit. Table passed to in-end would be set as environment for functions you declare in the classes, which isn't good I guess.

The everything-is-an-object is a good thing :)

On 27.01.2010 23:33, Richard Hundt wrote:
GrayFace wrote:
So far I've read mentions of several use cases, but I don't understand how exactly it may be useful in them. Here's what I read about: 1) Obvious usecase: something like Deplhi's 'with'. This is not the main use case Lua developers targeted, for sure. Inability to call global functions, a need for additional local variable to call methods makes this incomplete. 2) Modules. Maybe it's due to the fact that I didn't work with modules, but I don't see it useful. I don't see a reason for setting current environment like 'module' does in 5.1 or use of in-end of 5.2 due to the same reason: inability to call global functions. Generally, I'd prefer to directly write "function P.f() end" instead of "function f() end" given P is the module table.

2) Modules...

This is the one that I'm playing with atm.

Basically using environments to trap setglobal in class and module definitions to set up metatables: function definitions become virtual methods, "types" become attributes, constants become static members... etc.

It's mainly sugar, but you can get really close to Ruby's class and module semantics, and still have it look nice:

in module .Acme do
   in module .Friendly do
      function greet(self, whom) -- instance method (visible on mixin)
         print(" says: Hello "..whom.."!")
      function static.answer()   -- static method (doesn't mixin)
         return 42
   answer() -- 42 (static methods visible here)
   in class .Person do
      name = String -- instance attribute (type checked)
      function __init(self, name) = name -- TypeError if name is not a string

in class .Hacker (Acme.Person) do
   function __init(self, name)
      super.__init(self, name) -- "super" injected into environ

local bob ="Roberto")
bob:greet("World") -- "Roberto says: Hello World!"

I've got the above working (by breaking just about every rule: "module" is no longer a function, but a magic table, and all primitives have metatables, except for nil).

I appreciate it's not very Lua'esque to everything-is-an-object'ify, but I think it's pretty cool that you can create new syntax like that with just the native language features.



Best regards,
Sergey Rozhenko