Version française
Home     About     Download     Resources     Contact us    
Browse thread
The universal variable 'a would escape its scope
[ 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@m...>
Subject: Re: [Caml-list] The universal variable 'a would escape its scope
From: Christophe TROESTLER <Christophe.Troestler@umh.ac.be>

> I got a variant of the error
> http://caml.inria.fr/archives/200207/msg00110.html and I do not
> understand it.  Here is a small example showing it:
> 
>   # type 'a t = < output : (string -> unit) -> unit; .. > as 'a;;
>   type 'a t = 'a constraint 'a = < output : (string -> unit) -> unit; .. >
>   
>   # class a = object
>     method f : 'a. 'a t -> unit = fun o -> o#output print_string
>   end;;
>     method f : 'a. 'a t -> unit = fun o -> o#output print_string
>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   This expression has type < output : (string -> unit) -> unit; .. >
>   but is here used with type < output : (string -> unit) -> unit; .. >
>   The universal variable 'a would escape its scope
> 
> Is there something one can do about this?

The problem is that ['a. (< m : t; .. > as 'a)] is interpreted in a
special way, 'a meaning the row variable rather than the whole type,
and that does not work with constrained types.

The workaround is to use a class-type:

# class type t = object method output : (string -> unit) -> unit end;;
class type t = object method output : (string -> unit) -> unit end
# class a = object
    method f : 'a. (#t as 'a) -> unit = fun o -> o#output print_string
  end;;
class a : object method f : #t -> unit end

This does exactly what you want.

Jacques Garrigue