[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: multi-indexing tables (Re: Next Lua (5.2) feature request : slicing)
- From: Asko Kauppi <askok@...>
- Date: Sun, 26 Nov 2006 00:11:40 +0200
There's at least two issues I've faced, trying to pull this through
with token filters. ( I'd not be using it myself, but it's worthy a
try and good showcase for what syntax-aware tokening can do, and what
Multiple indices in value prefixes
"continuing" the tables in the way Lua normally does may turn out to
be a problem. The "prefix" part of a variable/expression takes care
of this and I'd like to not need to modify that part. In other words,
[1,2,..] would always need to be the _last_ part of an expression.
Also these would be invalid:
Is this okay?
It can be bypassed by the use of ():
(tbl[1,2,3]).xx -- is ok
Use in multiple assignment
tbl[1,2,3], anything= ... -- illegal
anything, tbl[1,2,3]= ... -- illegal
tbl[1,2,3]= ... --ok
If the replacement was merely tbl this would cause no
problems. But the big reason for doing the filter is also
performance, not mere syntax sugar. So instead tbl[1,2,3]= will be
converted into a function call, emulating the normal metatable etc.
stuff in Lua. Therefore, the assignment changes to a function call.
And that's not okay in multiple assignments.
Interesting, this is. And serves as a showcase also of the kind of
subtle problems that wider use of token filtering will bring along.
Perceived syntax won't any more be as rock solid, as it's with plain
Lua. A filter may have deficiencies that don't show up in the common
usage cases (= it is only designed for the common usage cases). Also,
error messages may be misleading to say the least! :) Though syntax-
aware filtering certainly helps make good error messages, if the
filter author just wants to do that.
On 25.11.2006, at 0.03, Asko Kauppi wrote:
Would you be okay in making this via token filters?
For the cases you point out, it could:
mat[exp1,exp2] --> getmetatable(mat).__index(exp1,exp2)
For a function returning multiple values, it could not, but we can
rule that out, right?
On 24.11.2006, at 21.51, Shannon Stewman wrote:
A straightforward extension to indexing that allowed a library to
implement mat[i,j] by allowing multiple arguments to __index and
__newindex. This would eliminate proxy objects (of the sort mat[i]
[j] requires) and not require painful manual addressing (the
highly annoying mat[j*NCOL+i]).
Slicing can easily be done with the existing syntax:
mat1[ slice(1,3) ] = mat2[ slice(4,6) ]
which extends nicely to 2+ dimensional arrays:
mat1[ slice(1,3) , slice(1,3) ] = mat2[ slice(4,6), slice(5,7) ]