Subtyping And Reflexion

While browsing the random pages, I encountered one of the Circle and Ellipse pages. I quickly scanned it, not expecting to find anything of interest; but lo and behold, I found a section where AlistairCockburn declares that there are no subtypes. I actually found this declaration interesting and examined the paper (at http://alistair.cockburn.us/index.php/Constructive_deconstruction_of_subtyping) he wrote to support his conclusion. I find that I am in total disagreement with him. In particular, his claim that the following statement breaks subtyping.

return ( argument.getClass().getName().equals ("T") );

I find this claim dubious. Now, if it is true that argument.getClass().getName() returns the same value for every instance of a type and this value is unique for every type, then it follows that each instance is of exactly one type. Clearly subtyping, where an instance must be both the subtype and the supertype and those type are distinct, cannot coexist with those assumptions. Luckily, we don't have to make those assumptions.

If we relax the second assumption, that the value returned is unique to each type, we find that we have to return the same value for every type in the transitive closure of any type of that instance. I.e. if foo :> bar and baz :> bar (Note the multiple supertypes), then every for, bar, and baz must all return the same value from the call to argument.getClass().getName(). This clearly weakens reflection to near uselessness, so we probably don't want to take this route.

If we relax the first assumption, that the same value is returned for every instance of a type, we find that subtyping and reflection can exist quite happily alongside each other. I.e. if foo :> bar, then arguments of type foo will either return "foo" or "bar". This preserves subtyping, and most of the power of reflection.


EditText of this page (last edited April 5, 2008) or FindPage with title or text search