subtypingとsubclassingの違い

h氏とメッセでの会話より

h> よく、インテリ君が「サブタイプとサブクラスは違う概念だからね」というのを見かけるがweb上に違いを説明している文章はあんまないです
kkanda> ないね
kkanda> それ、俺も不思議に思って探したんだけどあんままとまって書いてるところないね
h>今、探したけれど、日本語の解説文はないっぽい

ということで、h氏が海外のソースに当たってみた。
見つかったのがocamlのメーリングリストの過去ログA Theory of ObjectsのFAQ

どちらも、同じようなことが書いてあるのだがTheoryOfObjectsの方が明確に書いてあるので、そちらを引用すると、

Subtyping is a relation between (object) types, having to do with keeping track of the messages that objects can accept. Inheritance is a relation between classes, having to do with the superclass-subclass relation and with method reuse. The usual connection between inheritance and subtyping is the following: a subclass generates objects whose type is a subtype of the objects generated by a superclass.

subtypingというのは型と型との関係であり、subclassing(inheritance)というのは、classとclassの関係でありmethod(実装)の再利用を行うことである。ocamlのMLの方では、subtypingがsemantic conceptなのに対し、subclassingはsyntacticなものだと区別されている。

Initially, object-oriented languages confused classes with object types, and therefore ended up blurring the distinction between implementations (classes) and interfaces (object types), and between inheritance (code sharing) and subtyping (interface sharing). Recent object-oriented languages are designed to make these distinctions and to take advantage of them.

つまるところ、subtypingっていうのはinterfaceの継承で、subclassingというのはinterfaceの継承+実装の継承だというわけです。このFAQの著者は、最近のOO言語では型と実装がきっちりとまとまっていなのでsubtypingとsubclassingの違いがはっきりしなくて、もんもんとしてるわけです。

確かに、javaとかC#とかを見てるとsubtypingとsubclassingはそれほど明確ではないかもしれない。例えば、抽象メソッドのみを持つ抽象クラスはinterfaceと意味的には同じになるはずだが、言語の制約のため同じにならない。これは明らかに言語の実装にひきづられている証拠だ。(そういうときは素直にinterfaceを使いますが^^;)

ただ、型と実装をきちんと分離したプログラミング言語が本当に使いやすいかと言うとそうでもない気がする。型と実装を分離するには、mixinとinterfaceが使えればいいわけだがこれがフルにサポートされるとモジュール間の一貫性をメンテナンスするコストが高すぎる気がする。しかも、mixinを使って嬉しいモジュールってなんでしょう?