Table init syntax suggestion

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

Table init syntax suggestion

David Jeske-2
It occured to me that it would be really neat to be able to setup
fallback methods 'inline' in table creation.

For example, instead of doing:

a_tag = newtag();
settagmethod("gc",function () print("resource closed"); end);
a_table = { [1] = "foo",
            [2] = "bar" };
settag(a_table,a_tag);

It would be convinent to do:

a_table = { [1] = "foo",
            [2] = "bar",
            [`gc] = function () print("resource closed"); end };

Or something along those lines.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Table init syntax suggestion

Luiz Henrique de Figueiredo
>From: David Jeske <[hidden email]>
>
>It occured to me that it would be really neat to be able to setup
>fallback methods 'inline' in table creation.
>
>For example, instead of doing:
>
>a_tag = newtag();
>settagmethod("gc",function () print("resource closed"); end);
>a_table = { [1] = "foo",
>            [2] = "bar" };
>settag(a_table,a_tag);
>
>It would be convinent to do:
>
>a_table = { [1] = "foo",
>            [2] = "bar",
>            [`gc] = function () print("resource closed"); end };
>
>Or something along those lines.

how about this:

function T(a)
	local a_tag = newtag();
	if a._gc then settagmethod(a_tag,"gc",a._gc) end
	settag(a,a_tag)
	return a
end

a_table = T{ [1] = "foo",
            [2] = "bar",
            _gc = function () print("resource closed"); end };

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Table init syntax suggestion

David Jeske-2
On Mon, Oct 26, 1998 at 05:12:43PM -0200, Luiz Henrique de Figueiredo wrote:
> >It would be convinent to do:
> >
> >a_table = { [1] = "foo",
> >            [2] = "bar",
> >            [`gc] = function () print("resource closed"); end };
> >
> >Or something along those lines.
> 
> how about this:
> 
> function T(a)
> 	local a_tag = newtag();
> 	if a._gc then settagmethod(a_tag,"gc",a._gc) end
> 	settag(a,a_tag)
> 	return a
> end
> 
> a_table = T{ [1] = "foo",
>             [2] = "bar",
>             _gc = function () print("resource closed"); end };

That will work. I'll end up populating the "T" function will all
fallbacks.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]