lua-users home
lua-l archive

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

"type" is just a standard function of the base Lua API, so you can just override it to suit your needs. Moreover you can get inspiration from the tostring implementation which checks for the __tostring metamethod.

IMHO you should return exactly what __type metamethod returns, but the metamethod should return the user type as a second return value (the first being like the current type implementation). This will reduce problems with existing code that you wrote or borrowed from the net. However this may break some stuff where type return values are not implicitly adjusted to 1.

As a side note I also wondered how such user typing would be possible but never realized it was so straightforward until you asked :-)

-----Message d'origine-----
De : [] De la part de
Envoyé : 27 septembre 2006 08:02
À : Lua list
Objet : Re: __type for better typing

I second something like this would be useful.

My enum approach uses a __typeof() function within the 
metatable, which can be a constant string, too.   A well 
thought out system could be useful for tables, userdata and enums all of them.

Basically, I'd see this as a "subtype", and make type() return two values, instead of the one it now returns. 
 This way, compatibility hick-ups could be smaller, too?


On Wed, 27 Sep 2006 12:27:33 +0200
  Timm Felden <> wrote:
> Is there any *good* way to get the type of a table or userdata. I'm 
>thinking of types like "Vector" or "Sprite"
>or something else, that I am using internally. Currently I'm using 
>typechecking with the REGISTRYINDEX but I it doesn't work with my new 
>OOP System.
> So I came up with the idea to add an __type field to the metatable but 
>this would mean to me, that I have to implement a new gettype function. 
>Maybe someone has a better idea:)