lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Another option is:

    intf.method( obj )

In other words, treat the methods more like generic functions. Each of the
methods in the intf table could be the same piece of C code with a different
up value indicating what to dispatch to. For example, writing in Lua:

    function dispatch( obj, ... )
        local method_imp = obj[ method_id ] -- method_id is an up value
        assert( method_imp )
        return method_imp( obj, ... )
    end

Mark

on 9/26/05 5:51 AM, David Given at dg@cowlark.com wrote:

> On Saturday 24 September 2005 01:05, Eric Jacobs wrote:
> [...]
>> I have a situation similar to this. I've grown fond of the following
>> technique to call object methods. I think it's a big win once you get used
>> to its syntax:
>> 
>>         obj[intf.method]()
> 
> I quite like this --- it's elegant. Unfortunately, I doubt my users will
> agree... I suspect they'd prefer traditional syntax.
> 
> I think what I'll do is to have a table that represents each interface,
> containing bindings for the methods, created lazily, and then have a C
> dispatcher on the object that tries each interface in turn. I think in this
> case the reduced complexity outweighs the performance improvements in trying
> to do anything more complex; plus, with appropriate metatable abuse I can do
> the hard work in generating the bindings from Lua, which would be nice.
> 
> Thanks, everyone!