[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: __type for better typing
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Wed, 27 Sep 2006 11:19:45 -0400
"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 :-)
De : email@example.com [mailto:firstname.lastname@example.org] De la part de email@example.com
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 <firstname.lastname@example.org> 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
> 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:)