[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to prevent bugs without static type checking?
- From: Geoff Leyland <geoff_leyland@...>
- Date: Thu, 25 Oct 2012 06:31:23 +1300
On 24/10/2012, at 11:50 PM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
> And a devil in the details of argument checking; should the MT chain be
> traversed to see whether a subclass satisfies as a class higher up in the
> hierarchy (plant -> flower -> tulip, does a tulip satisfy an argument
> requiring a plant-type), maybe that requires an __istype() or similar
> function as well.
My take on this is [1]: give the object's metatable a __typeinfo field that's a table that has fields that are true if the object is of that type, so, for if value is a tulip:
getmetatable(value).__typeinfo -> { table=true, plant=true, flower=true, tulip=true }
and there's a typeinfo function so you can check with "if typeinfo(value).flower then...".
(Having said that, I'm not sure that long inheritance chains are a symptom of good practice)
[1] https://github.com/geoffleyland/rima/blob/master/lua/rima/lib/object.lua