First – QPen is in the “backward compatibility” folder – i.e., old name, don’t use it.
Pen is the right one.
Second – rdd is right that Pen defines *moveTo and this is not automagically translated to moveTo (no *).
A concept from object-oriented programming: “is-a” vs “has-a.”
Usually the first thought about how to extend the behavior of a class is to inherit from it. Then, the new class is of the same class as the original. In SC, Walker.new.isKindOf(Pen) == true.
This sometimes leads to trouble. So there’s another way: “has-a.” You write a class with no class-tree relationship to the one you want to extend, and in your class, you use the original class. Your class implements the interface that you need.
“Has-a” fits your use case a lot better than inheritance.
I suggest that if you delete : QPen and write the methods that you want using Pen (but not trying to “be” Pen), you will have a much easier time.
Thank you both for the cues!
I went for the “has-a”/Pen.moveTo solution and I have a working Walker now.
There are still a couple of things that are not clear to me though:
referring to
super is another name for this, it’s used to tell the interpreter to start the name lookup at this.class.superclass instead of this.class
when would you use super instead of this ?
There are a lot of class methods which refers to this as a class and not as an instance. How do you differentiate them to call a class method rather than an instance method? Signal for example: hanningWindow is a class method which returns a this.newClear filled with a sine, and newClear is a class method inherited from ArrayedCollection.
So why in my first attempt calling this.moveTo is automagically translated in moveTo (no star)?
Thank you so much for your inputs! You saved me a lot of time.
Second, super is used to explicitly call a method of the base class. Typically this is necessary if the subclass wants to extend/wrap a method of the base class. Example:
Foo : Bar {
doSomething { arg x;
super.doSomething(x); // call original method of Bar
// do some more things
}
}