lua-users home
lua-l archive

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

> I think the real problem is not when you use __typeof to report some
> custom type string, but rather when a userdata poses as one of the
> built-in types. When __typeof returns "table" ("string"), does the
> corresponding object really behave like a table (string)? Think of
> ipairs, pairs, get/setmetatable (all the string functions). True, in a
> modified Lua you can probably do all that, and adding support for
> __typeof seems like a minor issue in this situation, but what about
> stock Lua?

An additional library function, say realtype() (and the C equivalent)
would fix this easily. Also a warning in the documentation for the
__typeof metamethod should be placed saying that type masquerading can
lead to unusual bugs would be good enough. The Lua docs are very well
written, and finding information about such an error in the docs should
be easy. Plus, stock and custom error handlers could be written to check
for masquerading userdata/tables, and point out the real data type, say
instead of printing out "table value" we'd print out
"table(typeof:number) value", instead of "userdata value", print
"userdata(typeof:string) value".  That way we'd have a better idea of
what's truly going on in error messages. Or you could limit the return
values by throwing an exception any time a __typeof metamethod returns a
builtin type that doesn't match the real type. I'd think that would be
going overboard with it, but it is a potential solution.

> As useful as __typeof may be in a controlled for power-users, I guess
> it is this kind of thinking that makes the Lua authors hesitate about
> adding it to stock Lua. (Am I right here? ;)

Wouldn't doubt it.  :)  And I do agree that __typeof is a potential can
of worms, but then again, so are coroutines, the for statements, the
ability to redefine the global environment, and so many others. If the
criterion is whether the feature has a potential can of worms, Lua is
extremely lenient. Now there is no feature in Lua, to my knowledge and
limited understanding, that is a guaranteed can of worms.

> Just playing devil's advocate,

An awesome game to play. :)

"If Ignorance is Bliss, I'll take the Pain."