[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Question about metamethods
- From: Philipp Janda <siffiejoe@...>
- Date: Sun, 14 Feb 2016 14:26:42 +0100
Am 14.02.2016 um 13:41 schröbte Geoff Smith:
My preferred lua syntax mimics the C# line, so I would like to trigger a call to that function with something like
dgv1.Rows[1].Cells[4].Style.BackColor = "Yellow";
I suspect this probably isnt possible, can anyone prove me wrong ? Or alternatively suggest another variation on how to solve it ?
local PRIVATE, meta, unpack = {}, {}, table.unpack or unpack
local function tcopy( t )
local newt = {}
for k,v in pairs( t ) do newt[ k ] = v end
return newt, #newt
end
function meta:__index( k )
local u, l = tcopy( self[ PRIVATE ] )
u[ l+1 ] = k
return setmetatable( { [ PRIVATE ] = u }, meta )
end
function meta:__newindex( k, v )
local u, l = tcopy( self[ PRIVATE ] )
u[ l+1 ] = k
u[ l+2 ] = v
u.f( unpack( u, 1, l+2 ) )
end
local function newf( f, ... )
return setmetatable( { [ PRIVATE ] = { f = f, ... } }, meta )
end
-- test it:
local o = {
-- you can pass any function and it receives all keys and the
-- final value as arguments (print is just for show):
Rows = newf( print, "Rows" )
}
o.Rows[1].Cells[4].Style.BackColor = "Yellow"
Thanks Geoff
HTH,
Philipp