lua-users home
lua-l archive

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


I started to write this as an answer to the thread called "Surprising
behavior of table.insert()", but it quickly became something else -
so I am posting it as a new thread.

Please look at the example below. The function "id" is an easy way to
produce a "nullary list", as well as lists of any length.

  function id (...) return ... end
  print(id())                     -->
  print(id(nil))                  --> nil
  print(id(nil, nil))             --> nil nil
  T = {}
  table.insert(T, id())           --> "wrong number of arguments"
  table.insert(T, id(nil))        --> ok
  table.insert(T, id(1))          --> ok
  table.insert(T, id(1, 2))       --> ok
  table.insert(T, id(1, 2, 3))    --> "wrong number of arguments"
  assert(id())                    --> "bad argument... value expected"
  assert(id(nil))                 --> "assertion failed"
  assert(id(nil, nil))            --> "assertion failed"
  assert(id(nil, nil, nil))       --> "assertion failed"
  assert(id(1))                   --> ok
  assert(id(1, 2))                --> ok
  -- And now for the grand finale...
  print((id()))                   --> nil
  print((id(nil)))                --> nil
  print((id(nil, nil)))           --> nil
  print((id()))                   --> nil
  print((id(1)))                  --> 1
  print((id(1, 2)))               --> 1

That code was intented as an answer to something in the "Surprising
behavior..." thread - but then I realized that I have a question. In:

  id(1, 2, 3)

the "1, 2, 3" is _syntactically_ an "explist" - that terminology
appears at lots of places in the manual - but what is the term for the
corresponding thing in the _semantics_? Let me suppose, for a moment,
that it is "vlist" (for "list of values"); then functions always
receive vlists and returns vlists, and the function "id" above returns
exactly the same vlist that it received, even if it had length 0...

By the way, I just did a quick check on manual for lua-5.2.0-alpha -
just grepping for all occurrenced of "return" there and reading the
text around each one - and I got the impression that indeed there
isn't any short, official name for what I am calling a "vlist"; only
expressions like:

  values
  results
  return list
  returned values
  returns (as in: the returns of the called function)

and that lack of a distinctive name may be one of the reasons why so
many people fail to realize that vlists are practically first-class
values... the "practically" is because to store vlists into variables
we need to pack them, creating tables with an "n" field:

  function mypack (...) return {n=select("#", ...), ...} end
  function myunpack (T) return       unpack(T, 1, T.n) end  -- 5.1
  function myunpack (T) return table.unpack(T, 1, T.n) end  -- 5.2

  print(myunpack(mypack()))                  -->
  print(myunpack(mypack(nil)))               --> nil
  print(myunpack(mypack(nil, nil)))          --> nil nil
  print(myunpack(mypack(nil, nil, nil)))     --> nil nil nil

That's it for the moment.
Cheers,
  Eduardo Ochs
  eduardoochs@gmail.com
  http://angg.twu.net/


P.S.: I know that some people hate the idea of using tables with an
"n" field, and are more than willing to change the language completely
just to avoid it... But I think that _this particular discussion_ will
be more productive if I ask politely to them to not participate... So:
please? 8-)