Monkey C: How to simulate abstract static methods?

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 class A.

  • Inside doMore(), I want to call an abstract static method A.doSomething(), which would actually be implemented in each subclass, like B.doSomething().

  • So when I call B.doMore(), it should internally call B.doSomething() - not A.doSomething().

But when I try this:

  • Simply calling doSomething() inside A.doMore() just calls A.doSomething().

  • Using self.doSomething() doesn't solve it either.

  • And I can’t hard-code B.doSomething() in A, 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();
    }
}
  • I have static logic in an abstract class that is shared across all derived classes. However, it also needs to incorporate static behavior that differs depending on which derived class is calling the abstract class's static logic.

    Seems like you're trying to cram a square peg in a round hole.

    This wouldn't be possible in Java, either with abstract classes (as static methods can't be abstract) or with interfaces (as static methods in interfaces can't be overridden.)

    Ofc it would be possible in js or python, but Monkey C isn't any of the above languages.

    If you really need to use this pattern and you're willing to take the memory hit, you could implement a kind of frankenstein singleton pattern where every class which needs this kind of "static" behaviour has a spare instance which is dedicated to "static" behaviour which can also be extended in derived classes.

    The problem with this example below is that you can't actually use any "static" functions which depend on dynamic behaviour from a static context anymore (e.g. "A.doMore()" or "B.doMore()", you have to call them from the fake static instance.)

  • If you really need to use this pattern and you're willing to take the memory hit, you could implement a kind of frankenstein singleton pattern where every class which needs this kind of "static" behaviour has a spare instance which is dedicated to "static" behaviour which can also be extended in derived classes.

    That's an interesting approach - a bit too heavyweight for my use case, but I can see it working well in other contexts.

    In my case, I'm just trying to save a few lines of code, and there are simpler ways to handle it. My question was more out of curiosity and a desire to better understand how Monkey C works.

    I did try passing in the static class again, and it turns out it works now - not sure what I got wrong the first time. So I might just go with that solution after all.