lua-users home
lua-l archive

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


On 1/31/2010 6:24 PM, Peter Cawley wrote:
On Sun, Jan 31, 2010 at 6:14 PM, Norman Ramsey<nr@cs.tufts.edu>  wrote:
  >  Is it possible that 5.2 could allow syntax like:
  >
  >  table={
  >  function hello(self)
  >     doStuff(self)
  >  end,
  >  hi=6
  >  }

While this syntax might be slightly better than

     table={
     hello = function(self)
       doStuff(self)
     end,
     hi=6
     }

I don't see it as *enough* better to be worth adding extra syntax.
The syntactic sugar "function x" =>  "x = function" is meaningful at
the statement context, and is currently done at the statement context.
The sugar is not meaningful at the expression context, and is
currently not done at the expression context.
The sugar is meaningful at the field context, but is currently not
done at the field context.


Unless I misunderstand what you mean by "field context", I'll disagree with you. I think you mean for creating recursive functions in a table. So the function body needs to be able to refer to the same function (by the name of the table field). I don't see that as an obvious assumption, however.

    T = {
      function F(...)
        -- do stuff
        return F(...)
      end,
      -- why should that work if this doesn't?
      A = 'foo',
      B = A
    }

T.B, of course, is not 'foo' but nil. However, I think in-do can be used as a different kind of table constructor.

    local T = {}
    in T do
      function F(...)
        -- etc.
        return F(...)
      end
    end
    return T

Will almost work, except for not having access to globals. So then you do...

    local _g = _G
    local T = {}
    in T do
      function F(...)
        in _g do
          -- etc.
          return T.F(...)
        end
      end
    end
    return T

Notice that table fields have to be prefixed. How about if getenv were still around? Or to avoid confusion, the new function curenv that returns the current environment. You wouldn't need the upvalue. Moreover, could there be a function (or special token) that returns the current function? Then it becomes possible to write recursive function expressions and all this is simply...

    T = {
      F = function(...)
        -- etc.
        return curfunc()(...)
      end
    }

--
- tom
telliamed@whoopdedo.org