lua-users home
lua-l archive

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

Geoff Leyland <> writes:
>> Something like type predicates would be much better.  So the trick is,
>> assuming a standard set of type predicates, how do you extend them to
>> work on user types?
> Does the typeinfo function I suggested earlier count as a predicate?

As long as it's flexible enough to offer the same functionality... :)

Hmm, and I guess it can:

  function istype (obj, type)
    return typeinfo (obj)[type]

"typeinfo" does look like a nice idea, and seems to offer a clear way
to make the system extensible to user types, e.g. with a "__typeinfo"

> You could call it "istype" if that helped: istype(obj).typename.

I think "istype" intuitively takes two arguments, so better keep the
"typeinfo" name if that interface is thought preferable.

Since "istype" can be easily implemented in terms of "typeinfo", one
could offer the former function as an alternative interface to
typeinfo (which might be more readable in much code).

> One feature of returning at table is that the return value of typeinfo
> can be cached:
> local t = typeinfo(obj)
> if t.string then
>   ...
> elseif t.number then
>   ...
> elseif t.mytype then
>   ...
> end

Yeah, that's nice.


Virtues, n. pl. Certain abstentions.