niedziela, 24 sierpnia 2008

Wrap me!

Inheritance is fundamental way of creating new types in OOP. Apart of this, there is another important technique: composition, using many object to create higher level objects that provide interface to selected members of component objects. Inheritance is supported by many languages at syntax level, composition -- not!

Consider simple (real-world) example: we have GUI objects -- static label, and list box, we want another widget, labeled list box, a composition of these two simple widgets. We have to write tons of wrappers, that just call label or list methods. Here is example (C++):

class Label {
public:
void set_text(string s);
string get_text();
};


class ListBox {
public:
void clear();
int add_item(string s, bool uppercase);
string get_item(int index);
};


class LabeledListBox {
public:
void set_label(string s) {label.set_text(s);}
string get_label() {return label.get_text();}

void clear() {box.clear();}
int add_item(string s) {return box.add_item(s, false);}
int add_item_uppercase(string s) {return box.add_item(s, true);}
string get_item(int index) {return box.get_item(inex);}
private:
Label label;
ListBox box;

};

Of course C++ compiler is able to inline LabeledListBox methods, but programmer have to write many lines of code that merely call another methods. He must know types of arguments, returned values and other. And if some class is changed, he must update classes that use this one. He must maintain all these stupid wrappers. Wrappers are evil.

Wrappers are so frequently used in programming, that I'm still don't know why syntax of popular languages do not reflect this state. This is my view of syntax, but I hope shows the intention -- simple things should remain simple on screen:

class LabeledListBox {
public:
method set_label is label.set_text; // rename
method get_label is label.get_text; // rename

method clear, get_item from box; // publish two methods of box object
method add_item is box.add_item with uppercase=false; // create simple wrappers
method add_item_upperacase is box.add_item with uppercase=true; // with default arguments set to const

private:
Label label;
ListBox box;
};