[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: fun with table constructors
- From: "John Belmonte" <jvb@...>
- Date: Tue, 30 Jan 2001 18:54:02 +0900
In Lua the f{...}syntax (syntactic sugar for f({...})) is useful for data description:
SomeType {
1,
2,
3,
}
What if we want to allow the user to name this list data? One way is like this:
SomeType {
name = "abc";
1,
2,
3,
}
But that requires the user to remember to use ';' and do extra typing, and exposes internals of
the implementation. A syntax like this may be preferable:
SomeType "abc" {
1,
2,
3,
}
This can be done in Lua with a wrapping function that takes the name and generates a real
constructor. Here is an example:
function SomeType(name)
return function(t)
t.name = %name
return t
end
end
-John