[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua
- From: Dirk Laurie <dpl@...>
- Date: Sat, 17 Sep 2011 07:40:57 +0200
On Fri, Sep 16, 2011 at 10:12:50PM +0200, Javier Guerra Giraldez wrote:
>
> as much as i'd like to see real tuples in Lua (as the type of ... ),
Let me take this wish seriously, if only because I hate the kludgy
look of `select'#'`.
Syntax problems:
- We can use `(1,2,3)` for a tuple with elements 1,2,3, but we can't
use `(...)` to turn `...` into a first-class value because the
notation already means something else (the first element of `...`).
- We can use `[1,2,3]` for a tuple with elements 1,2,3, and by
analogy with tables would then want to write func[1,2,3], not
func([1,2,3]). But how does Lua know that we want to invoke
__call rather than __index?
Semantic problems:
- Suppose `a` is a tuple with elements 1,2,3, and `f` a function.
Is `f(a)` supposed to be equivalent to `f(1,2,3)`?
- `...` is a name for a group of entries on the local stack.
It is not a first-class value any more than `a` is a first-class
value when you say `local a`.
Solution:
Syntax:
- `tuple(...)` turns `...` into a first-class value.
- `totuple(a)` turns a proper list into a tuple.
Semantics:
- `tuple` is a userdata, with metamethods to implement __index,
__len, __concat (maybe more?)
Implementation:
- a nice little add-on module written in C
Wait — surely someone has done almost exactly this already?
Dirk
- References:
- Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua, Xavier Wang
- Re: Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua, Roberto Ierusalimschy
- Re: Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua, Xavier Wang
- Re: Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua, Roberto Ierusalimschy
- Re: Cons, tuple and __slots__ -- a simple mechanism to implement them in Lua, Javier Guerra Giraldez