1. Create classes:
Object subclass: #MyParentObject MyParentObject subclass: #MyChildObject 2. Create methods: MyParentObject>>myMethod ^ 1 MyParentObject>>myMethodVariant ^ super myMethod MyChildObject>>myMethod ^ self myMethodVariant 3. Evaluate: MyChildObject new myMethod And one million question is: Why I've got exception "MyChildObject does not understand myMethod"? -- Dmitry Zamotkin |
Dmitry,
On a quick glance, the part that looks odd to me is: > MyParentObject>>myMethodVariant > ^ super myMethod This is basically asking for Object>>myMethod. Does that help? Have a good one, Bill -- Wilhelm K. Schwab, Ph.D. [hidden email] |
In reply to this post by Dmitry Zamotkin-4
Dmitry,
This is a sneaky one (in all Smalltalks) that can be a bit disconcerting when you come across it. > 1. Create classes: > Object subclass: #MyParentObject > MyParentObject subclass: #MyChildObject > > > 2. Create methods: > > MyParentObject>>myMethod > ^ 1 > > MyParentObject>>myMethodVariant > ^ super myMethod > > MyChildObject>>myMethod > ^ self myMethodVariant > > > 3. Evaluate: > MyChildObject new myMethod > > And one million question is: > Why I've got exception "MyChildObject does not understand myMethod"? #self and #super are behave differently in respect to where they start searching for the method that should respond to a message. #self starts searching in the class of the object that recieved the message. If a suitable method is not found there then the superclass is checked - and so on. #super starts the search in the superclass of the class _containing_ the method (not the class of the object _receiving_ the message). So, when MyParentObject>.myMethodVariant is evaluated the search for the #myMethod method starts in the superclass of MyParentObject (i.e. Object) - which causes a walkback. To quote two lines from the BlueBook (pp62 and 63) that probably explain it better... (self) When a method contains a message whose receiver is self, the search for a method for that message begins in the instance's class, regardless of which class contains the method containing self. (super) However, when a message is sent to super, the search for a method does not begin in the receiver's class but instead, the search begins in the superclass of the class containing the method. -- Ian |
In reply to this post by Dmitry Zamotkin-4
Dmitry Zamotkin wrote:
> 1. Create classes: > Object subclass: #MyParentObject > MyParentObject subclass: #MyChildObject > > > 2. Create methods: > > MyParentObject>>myMethod > ^ 1 > > MyParentObject>>myMethodVariant > ^ super myMethod > > MyChildObject>>myMethod > ^ self myMethodVariant > > > 3. Evaluate: > MyChildObject new myMethod > > > And one million question is: > Why I've got exception "MyChildObject does not understand myMethod"? > > -- > Dmitry Zamotkin I checked this code and was surprised by Dolphin's behaviour. IMO ST/X does it better: - Compilation of MyParentObject>>myMethodVariant ^ super myMethod yields a warning "myMethod is not implemented in superclass chain" - Evaluating of MyChildObject new myMethod yields an unhandled exception "Object does not understand: myMethod" Dolphin compiles silently all the code and gives a "MyChildObject does not understand: myMethod" during evaluation of "MyChildObject new myMethod". This is somehow confusing. Maybe this happens due to optimizations by the compiler? Andreas |
Free forum by Nabble | Edit this page |