Structural Subtyping

Structural Subtyping is a form of typing where the subtype relationship is determined structurally. More specifically, one type A is a subtype of another type B iff A contains at least the same "stuff" as B (the same public methods, etc.) and has invariants which are at least as strong (in other words, if the invariant of A is satisfied, the invariant of B is also satisfied).

Very similar to (if not the same as) DuckTyping.

Many languages with DynamicTyping (such as SmalltalkLanguage, PythonLanguage, and RubyLanguage) have de facto structural subtyping, because of how typechecking works. Rather than explicitly checking the types of terms; these languages only check that method calls and member access "match"; so that the effective type of a method's argument is a phantom "interface" containing the union of all uses of that argument. This is true even though the above languages have nominative type systems (classes and such are assigned names) and nominative inheritance (all inheritance relationships are declared explicitly by the programmer).

Many functional languages with structural type systems quite naturally have structural subtyping.

The CeePlusPlus template system also is (sort of) structurally subtyped; even though the language proper is nominally subtyped.

JavaLanguage has no structural subtyping at all; all subtype relationships must be explicitly declared.

See also LatentTypesSmell, NominativeAndStructuralTyping.


EditText of this page (last edited August 1, 2005) or FindPage with title or text search