Hi, I am going to review the facilities provided by most object-oriented languages to add some methods to a type without subclassing nor accessing its inner implementation details. You can think of it as alternatives to the Adapter design pattern.
Let's say I have a type T and want to add a method to this type. For demonstration purposes, I will add a method called “universe” that will output the integer 42, which is, as Douglas Adams fans may know, the answer to life, universe and everything. Of course, in a real life program, one will add some more useful methods.
C# provides something called “Extension methods”. These are simple public static methods in public static classes that take a first parameter with the this keyword.
// implementation of T...
public static class Ext
public static int universe(this T t)
Then one may call the universe method as if it was part of the T class :
T t = new T();
int i = t.universe();
One could notice that we get rid of any mention of the Ext class. It is only here because of the dotnet limitation that forbids global functions. Every method must belong to a class.
D provides a simple yet powerful mechanism called Uniform Function Call Syntax (UFCS). Basically, any function f(a[, ...]) can be rewritten as a.f([...]).
// implementation of T
int universe(in T t) pure nothrow
T t = new T;
This is much simpler than C# Extension methods and more powerful as it does not only applies to classes or structs, but also to built in types. Andrei Alexandrescu used them on built-in arrays to get them the Range interface he had defined for containers (see std.array and std.range D modules for more information).
Objective-C provides a way to add methods at runtime to an existing class without subclassing. This is called “categories”.
@interface T : NSObject
// T fields
// T methods
// implementation of T methods
// Add a new Category of T with a universe method
@interface T (Universal)
- (int) universe;
- (int) universe
// all the instances of T in the process now have a universe method
T* t = [[ T alloc] init];
int i = [ t universe ];
Python being dynamic, it is very simple to add methods and fields to a class using the built in setattr function :
setattr(aClass, "universe", func)
t = T()
i = t.universe()
At this point, some of you may ask what is the point of doing all this ? These techniques can be very useful when doing some generic programming. Suppose you have written a very nice generic algorithm that works well with your types. And then someone provides you a new class that has similar abilities but a slightly different interface. Why would you subclass this new type ?
Inheriting a class creates a strong bind between a base class and its offspring that can later have consequences on the evolution of your code base. As you may have noticed, except Python, none of the programming languages presented here supports multiple inheritance...