lua-users home
lua-l archive

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


PA wrote:
> On May 30, 2007, at 23:13, John Labenski wrote:
>> So... can you determine if () was on the call to __(new)index and
>> that a function is expected?
> 
> No.
> 
>> Or that a '.' vs. a ':' was used?
> 
> No.
> 
> But never say never :P

Let's be a little more optimistic:

>> So... can you determine if () was on the call to __(new)index and
>> that a function is expected?

You can't automatically, but in your example, you could check if the key
string (the second argument to __index) starts with "Set" or "Get". If
the key is GetXXX, your return the getter function for XXX. If the
string is SetXXX, you return the setter function for XXX. Otherwise if
the string is XXX, you return its value. For __newindex there's no
possible confusion.

>> Or that a '.' vs. a ':' was used?

Actually I think you can, though I don't know and I'm not sure if you
can rely on it. IIRC the luaL_argerror function numbers the argument
differently whether you used . or : notation. But you can also and more
simply check the first argument of your setter. If it's a known
userdata, the user used : notation. If it's another type, he used the .
notation.

But I see no good reason to check for it. Static member functions should
assume that the . notation was used, and normal object methods should
assume that the : notation was used. That's how you write standard Lua,
check arguments normally and everything should be fine (ie if a user use
: with a static function the argument check code will quickly detect
it).