Reputation: 4830
Givetn two class names, className1
and className2
, how can I check if className1
has className2
anywhere in its heritage?
Upvotes: 0
Views: 109
Reputation: 137587
Alas, [incr Tcl] only supports introspection on objects and not on classes; you'll have to make an instance of className1
, fetch its heritage with info heritage $theInstance
, and check whether className2
is present in that list. Messy. (From 4.0 onwards you could use info class subclasses className2 className1
to check if className1
is a direct subclass of className2
, but that doesn't work for indirect subclasses, i.e., with some subclasses in between.)
However, general principles of OO programming in Tcl would tend to indicate that you should use duck typing if you can: don't worry about whether the object is of the right class, worry about whether it can respond to the messages you want to send to it (i.e., the methods you want to invoke). Since any object can trap attempts to invoke unknown method calls, you can't really find out what it will actually do by introspection, and have to Just Try It. Or look for some documentation if you're lucky.
Upvotes: 4