tables with multiple indeces?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

tables with multiple indeces?

Pavol Severa
there is probably nothing as funny as suggestions from a newbie - so
here is one:

if I got it right, an expression like t[1,2,3] is a syntax error.

would you find it reasonable if instead, whenever it meets a
comma-separated list as an index, lua would try to call the __index
(or __newindex) method of the metatable?


Reply | Threaded
Open this post in threaded view
|

Re: tables with multiple indeces?

Chris Marrin
Pavol Severa wrote:
there is probably nothing as funny as suggestions from a newbie - so
here is one:

if I got it right, an expression like t[1,2,3] is a syntax error.

would you find it reasonable if instead, whenever it meets a
comma-separated list as an index, lua would try to call the __index
(or __newindex) method of the metatable?

This WILL happen if you do this:

    t[{1,2,3}]

--
chris marrin                ,""$,
[hidden email]          b`    $                             ,,.
(650) 941-9040          mP     b'                            , 1$'
M1      ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'

Reply | Threaded
Open this post in threaded view
|

Re: tables with multiple indeces?

Gavin Kistner
On Mar 20, 2006, at 8:41 AM, Chris Marrin wrote:
This WILL happen if you do this:

    t[{1,2,3}]

As a silly idea, to simplify the syntax, how about:
t{ 1, 2, 3 }

Where 't' is actually a closure function wrapping up a table of your choosing?

function narray( ... )
	-- fill out your table here
	-- local _t =
	return function( ... )
		local indices = { ... }
		local theObj = _t
		for i=1,#indices do
			theObj = theObj[ indices[ i ] ]
		end
		return theObj
	end
end

local t = narray( 3, 3, 3 )
print( t{ 1, 2, 3 } )

Reply | Threaded
Open this post in threaded view
|

Re: tables with multiple indeces?

Chris Marrin
Gavin Kistner wrote:
On Mar 20, 2006, at 8:41 AM, Chris Marrin wrote:

This WILL happen if you do this:

    t[{1,2,3}]


As a silly idea, to simplify the syntax, how about:
t{ 1, 2, 3 }

Where 't' is actually a closure function wrapping up a table of your choosing?

Or t can still be a table, whose metatable has a __call method. This will call the method with a single table param. The question is what do you want to do with this. If you say:

    local a = t{1,2,3}

Then presumably you are dereferencing the table in some interesting way, and the __call function can do that for reasonable results. But this:

    t{1,2,3} = 12

is not legal Lua syntax. Unfortunately, a Lua function cannot return an 'lvalue' (in C nomemclature). Seems like it would be useful to allow a function call to be on the left side of an assignment statement. By default this:

    f(a,b) = 12+14

could translate to:

    f( {a,b}, {12+14} )

Using tables would make it easy to separate the input params from the right-side expression.

Just a thought...

--
chris marrin                ,""$,
[hidden email]          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'