interface と型チェックの有無 #2

なんというかなぁ、sumim さんの紹介された論文のタイトルが「Interfaces for Strongly-Typed Objecect-Oriented Programming」だというのを忘れていませんか。大学からダウンロード可能なので、ダウンロードしてちらっと見てみたけど、やっぱり interface において型は重要なんじゃないかなぁ。
適当に引用。

In this paper we develop a system of explicit interfaces for object-oriented programming. The primary goal for the system of object interfaces presented here is to allow interface compatibility to be checked at compile-time (eliminating the possibility of certain run-time errors) while preserving the power and flexibility of untyped object-oriented languages like Smalltalk.

1. Introduction

Interfaces for object-oriented programming are a valuable addition to the paradigm. By using the interpretation of interface as a type that formalizes the notion of an object protocol it is possible to develop a rich system of interfaces providing strong typing without compromising the programming model of untyped Smalltak.

6. Conclusion

Java における interface の立ち位置も基本的にこれとあまり変わらないと思う。interface inheritance を導入することで、implementation inheritance の関係が無くても subtype polymorphism が実現できるから、型チェックがありながらも Smalltalk のように柔軟なプログラミングができる、と(本当に Smalltalk 並に柔軟なのか、とか、それなら nominal subtyping より structual subtyping のほうが柔軟じゃないのかとかあるけど)。

例えばComparableというintefaceは「オブジェクト同士が比較可能である」という性質を表しています。これに型チェックの有無は関係ないですよね?PHPでも5.0からinterfaceが導入されましたが、型チェックのないPHPでもComparable interfaceは十分意味を持ちます。決して意味がないなんてことはありません(静的に型チェックができたほうが嬉しいという主張ならわかります)。

だから、「オブジェクト同士が比較可能である」型を表現/宣言できるところが Java の interface の重要な点なわけ。そりゃ、視点を変えれば比較のためのインターフェースを備えたオブジェクトは比較できるという性質を持っていると言えるだろうけど。というより、単に性質を宣言するだけなら、それってただの marker だよね*1

あと、PHP5 は type hinting が導入されていたりするので、型チェックがないということはない。もちろん、静的なチェックではないけど。

*1:メソッドの実装強制は型としての互換性保持のため