Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] implicit subtyping in method argument and parameterized class
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-05 (01:03)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] implicit subtyping in method argument and parameterized class
From: Pixel <>
> With ocaml 3.06 we are now able to replace
>     method with_foo : foo -> unit = ...
> with
>     method with_foo : 'a. (#foo as 'a) -> unit = ...
> and get implicit subtyping when calling with_foo.

Note that this implicit subtyping is essentially width-subtyping (the
object passed may have more methods), but not full subtyping (the
methods may not have subtypes).

> But I can't make it work when "foo" is a parametric variable:
>     method with_foo : 'foo -> unit = ...
> works whereas
>     method with_foo : 'a. (#'foo as 'a) -> unit = ...
> gives "Syntax error", and
>     method with_foo : 'a. ('foo as 'a) -> unit = ...
> gives "This type has no row variable"
> Is there a solution?

Class parameters are just type variables. You can unify them, but no
more. ('foo as 'a) is equivalent to 'foo constraint 'a = 'foo.

Note also that since the scope of 'a is the whole method type, this
doesn't make sense from a typing point of view.
What might make sense is:
  class ['with_foo] on_that_foo with_foo =
      method with_foo : 'with_foo = with_foo
  let o = new on_that_foo (fun x -> x#foo : 'a. (#some_foo as 'a) -> unit)
But this would require first class polymorphic functions, which are
not yet in the language. And if you want to share the code in the
class, you would also need subtyping between explicitly polymorphic
types, which is not there either.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: