A question for the Monkey C experts out there:
I'm looking for some advice on handling a design pattern issue in Monkey C.
Even though Monkey C doesn’t officially support abstract classes, I like to simulate them by defining "abstract" methods that simply throw an exception. This is especially handy when a method is supposed to return an object, and I don't want to create a dummy return value. With this approach, I can safely have code in the abstract class that calls the method, while ensuring that each subclass implements it properly.
This strategy works fine for instance methods.
However, I’m running into problems with static methods.
Here's the situation:
-
I have a static function
A.doMore()
in an abstract-like classA
. -
Inside
doMore()
, I want to call an abstract static methodA.doSomething()
, which would actually be implemented in each subclass, likeB.doSomething()
. -
So when I call
B.doMore()
, it should internally callB.doSomething()
- notA.doSomething()
.
But when I try this:
-
Simply calling
doSomething()
insideA.doMore()
just callsA.doSomething()
. -
Using
self.doSomething()
doesn't solve it either. -
And I can’t hard-code
B.doSomething()
inA
, because I want it to work generically across multiple subclasses.
Is this just a limitation of Monkey C’s type system and static method handling, or is there some workaround I'm missing?
Thanks for any insight you can share!
Below is some sample code illustrating the issue:
class A {
public static function doSomething() as Void {
throw new AbstractMethodException( "A.doSomething" );
}
public static function doMore() as Void {
// !! This is the issue !!
doSomething();
}
}
class B extends A {
public static function doSomething() as Void {
// this is what should get done
}
}
class C {
public function doItAll() as Void {
B.doMore();
}
}