* [Caml-list] implicit subtyping in method argument and parameterized class
@ 2002-09-04 23:18 Pixel
2002-09-05 1:02 ` Jacques Garrigue
0 siblings, 1 reply; 2+ messages in thread
From: Pixel @ 2002-09-04 23:18 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 532 bytes --]
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.
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?
thanks.
Full example:
[-- Attachment #2: implicit-method-argument-subtyping.ml --]
[-- Type: text/plain, Size: 1833 bytes --]
module Before_ocaml_3_06 = struct
class foo = object method foo = () end
class foo' = object inherit foo method foo' = () end
class on_any_foo =
object
method with_foo : foo -> unit = fun foo -> foo#foo
end
class ['foo] on_that_foo =
object
method with_foo : 'foo -> unit = fun foo -> foo#foo
end
class on_foo = [foo ] on_that_foo
class on_foo' = [foo'] on_that_foo
let _ =
let foo = new foo in
let foo' = new foo' in
let on_any_foo = new on_any_foo in
let on_foo = new on_foo in
let on_foo' = new on_foo' in
on_any_foo #with_foo (foo' :> foo) ; (* explicit subtyping *)
on_foo #with_foo (foo' :> foo) ; (* explicit subtyping *)
on_foo' #with_foo foo' ;
()
end
module Ocaml_3_06 = struct
class foo = object method foo = () end
class foo' = object inherit foo method foo' = () end
class on_any_foo =
object
method with_foo : 'a. (#foo as 'a) -> unit = fun foo -> foo#foo
end
class ['foo] on_that_foo =
object
method with_foo : 'foo -> unit = fun foo -> foo#foo
(* could be something like: *)
(* method with_foo : 'a. (#'foo as 'a) -> unit = fun foo -> foo#foo *)
(* and calling: [foo] on_that_foo *)
(* method with_foo : 'a. ('foo as 'a) -> unit = fun foo -> foo#foo *)
(* and calling: [#foo] on_that_foo *)
end
class on_foo = [foo ] on_that_foo
class on_foo' = [foo'] on_that_foo
let _ =
let foo = new foo in
let foo' = new foo' in
let on_any_foo = new on_any_foo in
let on_foo = new on_foo in
let on_foo' = new on_foo' in
on_any_foo #with_foo foo' ; (* implicit subtyping *)
on_foo #with_foo (foo' :> foo) ; (* explicit subtyping *)
on_foo' #with_foo foo' ;
()
end
[-- Attachment #3: Type: text/plain, Size: 76 bytes --]
--
programming languages addict http://merd.net/pixel/language-study/
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] implicit subtyping in method argument and parameterized class
2002-09-04 23:18 [Caml-list] implicit subtyping in method argument and parameterized class Pixel
@ 2002-09-05 1:02 ` Jacques Garrigue
0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2002-09-05 1:02 UTC (permalink / raw)
To: pixel; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2002-09-05 1:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-04 23:18 [Caml-list] implicit subtyping in method argument and parameterized class Pixel
2002-09-05 1:02 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox