lua-users home
lua-l archive

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


2013/5/12 Jean-Luc Jumpertz <jean-luc@celedev.eu>:
> And it would be much better if some type of hint about the foreseeable usage of a field could be passed as an extra parameter to the index metamethod in a future version of Lua. This would allow the index metamethod to return different (but related) data for a same key depending of the field expected usage.
>
> What do you think of it?

You don't always know at index time how the returned value will be
used. Consider for example:

local obj = get_one_of_your_objects()

local save = obj.save

--[[ do a lot of stuff here ]]

if some_unpredictable_result then
    save(obj) -- call method
else
    print(save) -- use property value
end

With your hack, it's no longer possible to call the method in that way
(which I admit is very unusual).

I don't know ObjectiveC, but from your description there doesn't seem
to be any perfect solution. IMHO you should keep using the objc
introspection mechanism you mention in the __index metamethod to
distinguish between properties and methods, and provide a fallback
mechanism for when it's not properly registered by the third party
object author (for example provide two functions
objc.get_property(obj, name) and objc.get_method(obj, name) in your
support module).