lua-users home
lua-l archive

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


One loses direct read/write access to attributes, but closure-based (which
is probably a better term than functional now that I think about it) can
certainly have data as closure values. In fact, this data is more private
and accessed more efficiently since it doesn't involve a table lookup.

Lacking a switch statement, however, method dispatch itself is slower.

function makeObject()
    local attribute = 1
    return function( msg, param )
        if msg == "attribute" then
            return attribute
        elseif msg == "setAttribute" then
            attribute = param
        elseif msg == "method" then
            -- method code goes here
        else
            error( "Unknown message: " .. tostring( msg ) )
        end
    end
end

In Lua as it presently exists, one needs to write:

    obj( "attribute" )
    obj( "setAttribute", 2 )
    obj( "method" )

With the proposed change one could write:

    obj:attribute()
    obj:setAttribute( 2 )
    obj:method()

Now, it is true that this also enables one to write

    print:hello()

but it doesn't create pressure to do so.

Mark

on 9/22/04 12:43 PM, Bilyk, Alex at ABilyk@maxis.com wrote:

> And still this would only apply to those 'objects' that don't have any
> attributes.
> 
> given a table 'object'
> 
> table_object =
> {
> method = function () end,
> attribute = 1
> } 
> 
> table_object.attribute = 2
> table_object : method()
> 
> How can one convert this into a functional one? So, the proposed feature would
> only apply to objects that effectively are bags of functions with no data.
> Right? I think, practically, most objects people are dealing with have both
> methods and attributes. Given an function based object, can one use attributes
> with them?
> 
> like 
> 
> function_object:method() --> function_object("method")
> function_object.attribute --> ???