Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type from local module 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: Virgile Prevosto <virgile.prevosto@m...>
Subject: Re: [Caml-list] Type from local module would escape its scope?
Le lun 03 jui 2006 15:19:35 CEST,
Bruno De Fraine <Bruno.De.Fraine@vub.ac.be> a écrit :

> module type FOO =
> sig
>    type t
>    val value : t
> end ;;
> let foo (ignore: 'a -> unit) =
>    let module Foo : FOO =
>    struct
>      type t = int
>      let value = 1
>    end in
>    ignore Foo.value
> ;;
> 
> With an error on the expression "Foo.value" stating that "The type  
> constructor Foo.t would escape its scope". Reading about the typical  
> case for this error message in http://caml.inria.fr/pub/ml-archives/ 
> caml-list/2002/10/0cf087feab3ef8dc5ccba5a8592472fb.en.html didn't  
> really help me. Why does it make a difference whether ignore is an  
> argument?
> 

Because "'a -> unit" does not mean the same thing in both cases. In the
case of Pervasives.ignore, it is a type scheme which denotes all the
types you can obtain by instantiating 'a. On the contrary, when used as
a type annotation to your argument, "'a -> unit" only tells ocaml that
there exists an 'a such that the argument ignore has this type: you can
see that with the following code:

# let foo (ignore: 'a -> unit) = ignore 1;;
val foo : (int -> unit) -> unit = <fun>

where 'a is instantiated by int in the inferred type.

IIRC arguments can not have a generalized type of the form
"forall 'a, 'a -> unit", but methods and record fields support such
types: for instance, you can have:

# module type FOO =
sig
   type t
   val value : t
end ;;
        module type FOO = sig type t val value : t end
# let foo (ignore: <call: 'a.'a -> unit>) =
  let module Foo: FOO = struct type t = int let value = 1 end in
  ignore#call Foo.value;;
val foo : < call : 'a. 'a -> unit > -> unit = <fun>

-- 
E tutto per oggi, a la prossima volta.
Virgile