lua-users home
lua-l archive

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

El 15/09/2005, a las 18:53, Lucas Ackerman escribió:


I don't have any great syntax ideas. <> are already used in operators,
and [] in table indexing.  Maybe \{}.

Currently Lua defines function calls as (pasted from the manual):

  functioncall ::= prefixexp args
  functioncall ::= prefixexp `:´ Name args

If tuples are really desired (my vote is "yes" for inmutable tuples), the grammar might be changed to:

  functioncall ::= prefixexp exp
  functioncall ::= prefixexp `:´ Name exp

This way functions would only have a single argument, BUT that argument could be a tuple by defining:

  expt ::= tuple
  tuple ::= '(' [explist1] ')'

This when you call a "N-argument" function what you are doing is passing N-tuple to a function that only receives a tuple as single argument. Some functional languages (ML, OCaml, Haskell...) work like that. So the following are more orthogonal:

  function_a "a string argument"
  function_b { "table argument" }
  function_c ("tuple", "argument")
  function_d 1762

  (now they're all one-argument functions -- prefixexp exp)

But there's a "gotcha": function definition. For example:

  function foo (x, y)
    -- mumble with 'x' and 'y' here

  function bar (z)
    -- do something with 'z' here

  function bar2 z
    -- do something with 'z' here

The first one is easy: it's a function that will receive a 2-tuple as argument, but the second one is unclear. It can be a function that receives a single argument, OR a function that receives as argument a tuple with only one element!! One possibility is the third example (so there's no ambiguity between single argument functions and the 1- tuple argument.

Moreover, in the first function Lua would need to do some kind of pattern-matching to assign names to the elements of the tuple passed as argument. Pattern matching is often ignored but a always found it incredibly useful when programming with functional languages (mainly OCaml). I'm not suggesting to add full-blown pattern matching to Lua, but matching tuples and tables at least would be very interesting:

  v1 = { x = 1, y = 4 }
  v2 = { x = -9, y = 6 }

  function vector_add ({x1 = x, y1 = y}, {x1 = x, y2 = y})
    return { x = x1+y1, y = y1+y2 }

  vector_add (v1, v2)

...and so on. (Note: read this as "function vector_add matchs a 2- tuple, whose 1st element is a table which has the fields 'x' and 'y' assigned as local names 'x1' and 'y1'; and the 2nd element is a table with fields 'x' and 'y' assigned as local
names 'x2' and 'y2'"...)

(I know the example could be better, but it's... just an example :-P )

Final words: don't take this stuff too seriously. I'm just writing as thoughts come to me...

Best regards,

Adrián Pérez
GPUL/CLUG member »
"Experience is what you get when you don't get what you want." (Dan Stanford)

Attachment: PGP.sig
Description: Mensaje firmado digitalmente