lua-users home
lua-l archive

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

On 7-Sep-04, at 3:45 PM, Eric Tetz wrote:

Asko Kauppi wrote:
What do you think, or should I just shut up? :)

I've always thought it would be nice to have some syntatical sugar for
a closure, especially where there are no parameters.

In a way, it is unfortunate that the same keyword is used both for function statements and for function constructors, although no syntactic ambiguity arises. However, this does force the use of () to indicate that the closure has no arguments. Moreover, it makes it more complicated to allow the use of free-standing closures as parenthesis-free arguments, although that is possibly not a good idea.

I don't think overloading "do" is great, either. Do (at least in English) implies a certain urgency; one would naturally expect the chunk to be "done", rather than stored for later usage. Writing closures as "function() ... end" is only slightly wordier.

If I had one syntactic sugar wish with respect to functions, it would be to allow the use of function [:]key(arg) ... end in a table constructor, so that one could write, for example: (Quoting something I see in front of me)

function Memoise(fn)
  return withmeta {
    function :__index(key)
      if key then
        local rv = fn(key); self[key] = rv; return rv
    function :__call(key)
      local rv, err = rawget(self, key)
      if rv == nil then
         rv, err = fn(key)
         self[key] = rv
      return rv, err

rather than the somewhat clumsier version with __index = function(self, key) ...

  function withmeta(meta) return setmetatable({}, meta) end

  purely for syntactic convenience :)