On Thu, 24 May 2007, LORENZO wrote:
> Hello,
>
> I have studied a example from OCaml system manual,
> and cannot get throuth idea of "keeping polymorphism"
> as following:
> #type idref = { mutable id: 'a. 'a -> 'a } ;;
>
> I don't understand why there is a dot
> between 'a. 'a -> 'a
> In my understanding, it's the same thing with
> #type 'a idref = { mutable id: 'a -> 'a } ;;
>
> Does the dot notation serve for any purpose else?
Yes. It keeps the 'a from becoming confined to a single type when you
create a value of type idref. For example:
# type idref = { mutable id: 'a. 'a -> 'a } ;;
type idref = { mutable id : 'a. 'a -> 'a; }
# type 'a idref' = { mutable id': 'a -> 'a } ;;
type 'a idref' = { mutable id' : 'a -> 'a; }
# let id = {id = fun x -> x};;
val id : idref = {id = <fun>}
# let id' = {id' = fun x -> x};; (* note the monomorphic type signature *)
val id' : '_a idref' = {id' = <fun>}
# id.id 1;;
- : int = 1
# id.id "a";;
- : string = "a"
# id'.id' 1;;
- : int = 1
# id';; (* note that id' is now restricted to type int idref' *)
- : int idref' = {id' = <fun>}
# id'.id' "a";;
Characters 8-11:
id'.id' "a";;
^^^
This expression has type string but is here used with type int
William D. Neumann
---
"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.
Tigers are noble and sleek; children are loud and messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
.