Givetn two class names, className1 and className2, how can I check if className1 has className2 anywhere in its heritage?
Givetn two class names, className1 and className2 , how can I check if className1
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Alas, [incr Tcl] only supports introspection on objects and not on classes; you’ll have to make an instance of
className1, fetch its heritage withinfo heritage $theInstance, and check whetherclassName2is present in that list. Messy. (From 4.0 onwards you could useinfo class subclasses className2 className1to check ifclassName1is a direct subclass ofclassName2, 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.