オブジェクト指向プログラミング vs 型マッチングシステム
http://d.hatena.ne.jp/w_o/20070422#p3
を読んで思ったこと。
例えば、JavaのTreeSetを例にすると、
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>, SortedSet<E>
...
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in ascending order.
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
an iterator over the elements in this set.のような形で色々なinterfaceの多重継承で何とかしている問題。
C++のGenericsでは、
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last) {のように書いているもののRandomAccessIteratorとして渡された型が実はForwardIteratorだったら、templateの実体化の段階でコンパイルエラーになり、判りにくい。
で、Javaの例がGenericsとオブジェクト指向的な手法なのに対して、C++0xのconcept*1は型マッチシステム・・・と今俺が命名。正式な用語だれか教えて。
型マッチシステムのがJava的オブジェクト指向+Genericsより優れているところは、
1. 継承関係がシンプルになる
2. マッチを試す順序を指定することで、よりよいアルゴリズムを自動的に選択できる
・・・かな。遅刻しそうなので続きは後で考える。
*1:http://d.hatena.ne.jp/Cryolite/20070301#p1に例がある