Version française
Home     About     Download     Resources     Contact us    
Browse thread
bizarre type
[ 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] bizarre type
From: Damien Doligez <damien.doligez@inria.fr>

> If I understand correctly, this is your problem:
> 
>          Objective Caml version 3.08.3+4 (2005-06-21)
> 
> # type 'a marshalled=string;;
> type 'a marshalled = string
> # let make (x : 'a)=(Marshal.to_string x [] : 'a marshalled);;
> val make : 'a -> 'a marshalled = <fun>
> # make 1 = make "foo";; (* int marshalled is the same as string 
> marshalled *)
> - : bool = false
> 
> It works better you use a concrete type instead of an abbreviation:
> 
> # type 'a marsh2 = Marsh of string;;
> type 'a marsh2 = Marsh of string
> # let make2 (x : 'a) = (Marsh (Marshal.to_string x []) : 'a marsh2);;
> val make2 : 'a -> 'a marsh2 = <fun>
> # make2 1 = make2 "foo";; (* int marsh2 is not the same as string 
> marsh2 *)
>              ^^^^^^^^^^^
> This expression has type string marsh2 but is here used with type int 
> marsh2

Actually this is not a very good suggestion from the point of view of
safety, as this useless parameter can still be modified by subtyping:

# make2 1 = (make2 "foo" :> _ marsh2);;
- : bool = false

If you care about safety, you must either use an abstract type, or a
private type in ocaml-3.09 (not 3.08!).

On the other, if you want to keep the possibility of overriding the
parameter, while detecting non-annotated cases, this may actually be a
good approach.

Jacques Garrigue