Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] implicit subtyping in method argument and parameterized class
From: Pixel <pixel@mandrakesoft.com>
> 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?

No.
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 =
    object
      method with_foo : 'with_foo = with_foo
    end
  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.

       Jacques
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners