lua-users home
lua-l archive

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


On 5/30/07, Jerome Vuarand <jerome.vuarand@ubisoft.com> wrote:
PA wrote:

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.

Yeah, I'd thought of that, but was hoping to get the info directly
from lua. This requires a little bit of string parsing, but that
shouldn't be too much of a slowdown.

>> 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

I will look into this tonight.

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.

I guess I could check the number of parameters and if +1 greater (but
note, the first param may actually want an instance of the same class)
and the first is of the right userdata type just remove it. Or,
perhaps more safely, store the class instance as an upvalue (if not
static) and if +1 parameters and the first item is the same userdata
as the stored upvalue, remove it.

Both of these ways are not flawless at properly detecting user error,
but should work almost all of the time.

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).

That is the current plan. I was just trying to nice by allowing an
instance of the class to use the ':' notation for static functions as
you would for regular member functions. I want there to be as few
little "gotcha's" as possible so you don't have to constantly refer to
the docs to see if a function is static or not.

Thanks,
   John Labenski