lua-users home
lua-l archive

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

On Sat, Sep 25, 2010 at 8:56 AM, Rebel Neurofog <> wrote:
> Have you already discarded this way?


This is a nice technique/discipline, that among other things, bypasses
the colon usage. You can also uses the __call metamethod to have
similar effect. Lua is really great.

But I think it could be much better at a very low price: a true bargain.

The OOBit patch aims to create a lower cost, lower level native
mechanism (independent of programmer's skills and discipline) that
enhances Lua´s OO abstraction, for example,  hiding "implementation
details" such the colon usage in function calls.

By the way: Please, forget the implementation steps (and oocall
function) listed in original message. I think there is a faster and
more transparent way to implement the oocall semantic.

To exemplify what could be done, suppose - it is not a proposal - that
Lua offers the METHOD keyword as a syntactic sugar for ooset( function
() ... end ), or consider "NEW stat" as syntactic sugar for ooset(
stat ), a more versatile approach.

So you could write:

Account = {}

function Account.create()
  local t = new {_balance = 0}  -- sets OOBit of table .May be useful
- it is yet an open question.
  return setmetatable(t,{ __index = Account})

function Account:deposit (v)  -- colon syntactic sugar
  self._balance = self._balance + v

method Account.withdraw (v)  -- using method keyword
  self._balance = self._balance - v

new function Account.balance(v)   -- using new keyword
  return self._balance

local a = Account.create()

a:deposit(100)    -- using colon
a.deposit(a,50)   -- explicit self

-- using dot
print(a.balance())  --

-- issues
a.deposit(10)    -- missing self - programmers fault: typical error of
javascript or eventual Lua programmer. No help from Lua.
a:deposit(a,10) -- too much "selfs"
a:withdraw(20) -- using colon with method: the patch will print a
error or ignore the extra self (open question)

-- better semantics
do local f = a.deposit;  f(20) end --  missing self. Lua cannot do
anything special to help
do local f = a.withdraw;  f(20) end --  the patch should print: method
call requires a table (object?)
do local f = a.withdraw;  a.f(20) end --  ok

    local table = {}
    table:proc = function() ... end -- You cannot do this today.
    table.proc = method() ...end  -- this would be ok
    table.proc = new function () ...end  -- this would be ok

  table = {
    method (...) ... end, -- ok.
    new function (...) ... end, -- ok
    function (self, ...) ... end, -- ok (explycit self - lower abstraction)
    -- function : (...) -- no syntact sugar here.

-- table[1]('test') <=> do local f = table[1]; f.(test') end

As a last thought, just for fun:

If we can set OOBit of value of type string, you could write something like:

local oldlower = classes.string.lower

classes.string.lower = function()
  return "OO:" .. self.oldlower();

local s = new "test" -- using new constructor
print (s.lower())