Do I understand correctly that Liskov Substitution Principle cannot be observed in languages where objects can inspect themselves, like what is usual in duck typed languages?For example, in Ruby, if a class B inherits from a class A, then for every object x of A, x.class is going to return A, but if x is an object of B, x.class is not going to return A.Here is a statement of LSP:Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.So in Ruby, for example,class T; endclass S < T; endviolate LSP in this form, as witnessed by the property q(x) = x.class.name == 'T'Addition. If the answer is yes (LSP incompatible with introspection), then my other question would be: is there some modified weak form of LSP which can possibly hold for a dynamic language, possibly under some additional conditions and with only special types of properties.Update. For reference, here is another formulation of LSP that I've found on the web:Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.And another:If S is a declared subtype of T, objects of type S should behave as objects of type T are expected to behave, if they are treated as objects of type T.The last one is annotated with:Note that the LSP is all about expected behaviour of objects. One can only follow the LSP if one is clear about what the expected behaviour of objects is.This seems to be weaker than the original one, and might be possible to observe, but I would like to see it formalized, in particular explained who decides what the expected behavior is.Is then LSP not a property of a pair of classes in a programming language, but of a pair of classes together with a given set of properties, satisfied by the ancestor class?  Practically, would this mean that to construct a subclass (descendant class) respecting LSP, all possible uses of the ancestor class have to be known?  According to LSP, the ancestor class is supposed to be replaceable with any descendant class, right?Update.  I have already accepted the answer, but i would like to add one more concrete example from Ruby to illustrate the question.  In Ruby, each class is a module in the sense that Class class is a descendant of Module class. However:class C; endC.is_a?(Module) # => trueC.class # => ClassClass.superclass # => Modulemodule M; endM.class # => Moduleo = Object.newo.extend(M) # oko.extend(C) # => TypeError: wrong argument type Class (expected Module)
Is Liskov Substitution Principle incompatible with Introspection or Duck Typing?
object oriented;dynamic languages;liskov substitution
Here's the actual principle:Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.And the excellent wikipedia summary:It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).And some relevant quotes from the paper:What is needed is a stronger requirement that constrains the behavior of sub-types: properties that can be proved using the specification of an objects presumed type should hold even though the object is actually a member of a subtype of that type...A type specification includes the following information:   - The types name;   - A description of the type's value space;   - For each of the type's methods:    --- Its name;   --- Its signature (including signaled exceptions);   --- Its behavior in terms of pre-conditions and post-conditions.So on to the question:Do I understand correctly that Liskov Substitution Principle cannot be observed in languages where objects can inspect themselves, like what is usual in duck typed languages?No.A.class returns a class. B.class returns a class. Since you can make the same call on the more specific type and get a compatible result, LSP holds. The issue is that with dynamic languages, you can still call things on the result expecting them to be there.But let's consider a statically, structural (duck) typed language. In this case, A.class would return a type with a constraint that it must be A or a subtype of A. This provides the static guarantee that any subtype of A must provide a method T.class whose result is a type that satisfies that constraint.This provides a stronger assertion that LSP holds in languages that support duck typing, and that any violation of LSP in something like Ruby occurs more due to normal dynamic misuse than a language design incompatibility.