lua-users home
lua-l archive

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


Hi,

after a really nice discussion on irc://irc.freenode.net/#Lua I'd like to present my view on this topic in a more "conserving" medium than an IRC session.

We discussed how to append things to an array and how to get the length of an array.

My approach is a bit broader and comes from the Ada programming language. I'd like to add some "attributes" to each value. These attributes represent meta-data about the values, such as the type or a table's size.

I chose the ASCII character #39 (single quote) for access to the attributes of a type, because that's the way it looks like in Ada. The choice for the single quote has been made because it looks like a vertically mirrored dot. The dot represents access to the value's data, while the single quote represents access to the meta-information, which is kind of othogonal, hence the vertical mirroring.

Here is an example of how the code might look like.

table = {1, 2, 3, 4}
print table'lastindex
-- The number of elements stored in the array (read-only)

print table'size
-- The number of elements stored in the table (array + hash)

table'after = "first"
-- Append "first" to the table; table'after is a write-only lvalue

print table'type
-- print "table"


So far for the attributes of the tables. We should discuss which attributes are suitable for each of the builtin Lua types.

all values:
  type      -- the type as returned by the type() function
  image     -- the value of the object as a string, appropriate
            -- for being parsed by the Lua parser
            -- (might be a problem for recursive tables and userdata)

nil, boolean, number, userdata:
  -- no additional attributes

number:
  maxvalue  -- DBL_MAX for double, INT_MAX for int, ...
  minvalue  -- DBL_MIN for double, INT_MIN for int, ...
  delta     -- the minimum increase: 1 for int

string:
  length    -- see string.len()

function:
  env       -- see getfenv()

thread:
  status    -- see coroutine.status()

table:
  lastindex -- A number n for which table[n] ~= nil and table[n] == nil
  after     -- A write-only lvalue that represents
            -- table[table'lastindex + 1]
  meta      -- see getmetatable()
  ipairs    -- see ipairs()
  pairs     -- see pairs()


So far for now.

Roland