Partial Functions and Enforcing Extension
October 10th, 2009I like playing around with other programming languages from time to time, just to see how or what they do differently than the core languages that I work with every day. So this morning I was spending some time with C#. I’ve actually done some development in C# in the past. About three years ago now, I think it was that long ago…maybe longer, I used it to develop both a Sprite Editing tool and a Level Designer. So I’m “familiar” with the language, but basically have etch-a-sketched what makes it unique from Java.
My rediscovery of C# got me thinking about function modifiers, mainly because C# and Java have countering approaches to handling virtual functions. In Java your a virtual function unless declared otherwise by using the final modifier (private methods are probably not considered virtual either). But in C# it’s the exact opposite. Your virtualality (is that a word?) needs to be explicitly declared by using…wait for it…the virtual modifier.
Another difference is in the subclasses. Again, C# seems to be much more explicit about this kind of stuff. You must use the override modifier to declare that your overriding a virtual function. In Java you really don’t have to do anything, just make sure you have the same name and parameters. Sure, that @Override thing’s been tacked on to newer versions of Java. This helps the compiler make sure that the virtual method actually exists in the superclass, but you don’t have to use it if you don’t want to. C# makes you use it, which in turn ensures of your intentions.
Are these differences a big deal? Not really. My preference would probably be with the way C# accomplishes it, as it allows you to be more explicit in your intentions without being anymore verbose about it, which results in clear, easier to maintain code.
But the purpose of this post is where my mind went when considering the way virtual functions are handled in both languages. There happens to be a big hole in the amount of control that the superclass can take over its virtuals. In both languages, actually any language I’ve worked with for that matter, the superclass has little to no control on whether or not the subclass inherits the functionality provided by a virtual function. If the subclass feels it needs the superclass’s implementation as well as some additional stuff, then it can easily call upon the provided code by doing super.FuncName() in Java or base.FuncName() in C# within the overridden function. But, what if the superclass wants to force the subclass to use the code its virtual function has provided? What if it just wants to provide the subclass with the ability to extend a method and not completely override it? Both languages need what I’m calling, the partial function modifier.
For those that haven’t stop reading, a partial function would be a function declared and defined by the superclass that provides some functionality that needs to be apart of every subclass’s implementation of that function. The subclass would only be able to extend, using the extend modifier of course, the function and provide further logic that is specific to the subclass’s needs. Similar to an abstract class, the partial function would be required to be extended, or have its class marked abstract, as by marking a function as partial the superclass is basically saying, “Hey, this method, the code it implements…it’s required but the function is still unusable unless you provide the functionality needed to complete it.”
Here’s a usage example to show…what else, it’s usefullness. Let’s say you have an abstract GameState class. This class should be inherited by every different game state that your game has. You’d probably have a subclass for the main menu, gameplay, submitting highscores, and so forth and blah.
The GameState class will need to define methods for loading, drawing and updating. Each method will need to be implemented by every subclass. You start off by making all three of the methods abstract. But then you start creating some subclasses and realize that each and every call to Update should query for actions by the user. Did they press a key, did they select something…stuff like that. So you turn the Update method into a virtual method (lets think in C# on this one) and implement the querying functionality for the user’s actions. Now each subclass just needs to override Update and call base.Update() within it in order to take advantage of the functionality provided by the superclass.
That seems like a perfectly acceptable way of implementing the Update function. But what ends up happening from then on is that every time you create a new game state and go about setting up your Update method, you forget that you need to make the base.Update call. So you start testing the new state, no user input works and you smack yourself in the head for forgetting that same stupid call once again. Now if you had a partial function, the burden is on the superclass to make sure that that part of the code is implemented every time. And the superclass has no problem with that…mainly because, well, it’s SUPER. Sorry, that’s lame but I’m leaving in it.
Anyways, would a partial method modifier really save you oodles of time…probably not. But the point is more that the superclass should be able to control this kind of circumstance, not the subclass. And right now the subclass is piloting the ship. Partial methods ensure that the vital code of a method, and we can assume that it is vital because otherwise there wouldn’t be a reason to force the subclass into using it, is being run before anything else the subclass might tack on to execute. A little more complexity can be hidden and a little less burden can be put on the subclasses, neither of which are ever bad things.
I’d love to hear what I’m missing on this idea or why it wouldn’t work or how stupid I am, or even if there is a language out there that already implements such a feature. Any conversation on this would be good. Let me know what you think.
