■
http://jijixi.azito.com/cgi-bin/diary/index.rb?date=20051122#p06
ほんと、すみませんorz
で、こんなんでどうでしょう。
# class eq = object (self:'a) method eq (x:'a) = not (self#neq x) method neq (x:'a) = not (self#eq x) end;; class eq : object ('a) method eq : 'a -> bool method neq : 'a -> bool end # class foo (x:int) = object (self:'a) inherit eq method getter() = x method eq (y:'a) = x = (y#getter()) end;; class foo : int -> object ('a) method eq : 'a -> bool method getter : unit -> int method neq : 'a -> bool end # let a = new foo 3;; val a : foo = <obj> # let b = new foo 3;; val b : foo = <obj> # a#eq b;; - : bool = true # a#neq b;; - : bool = false # class bar (x:float) = object (self:'a) inherit eq method getter() = x method eq (y:'a) = x = (y#getter()) end;; class bar : float -> object ('a) method eq : 'a -> bool method getter : unit -> float method neq : 'a -> bool end # a#eq (new bar 3.0);; This expression has type bar but is here used with type foo Types for method getter are incompatible
baseクラスはなくても大丈夫になりました。(self:'a)がうざいっちゃうざいんですが、まあ許容範囲内でしょうか?メソッドの型指定は、このメソッドが多相型なのでどうしても必要になります。*1
ちなみに、この辺はhttp://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:binary-methodsに書いてあります。
*1:higher-rank polymorphismと言うらしいです。と昔さかいさんに教えていただきました。