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: Damien Doligez <damien.doligez@i...>
Subject: Re: [Caml-list] bizarre type
On Jun 30, 2005, at 23:42, Julien Verlaguet wrote:

> In fact it prevents me from writting this :
>
> type 'a marshalled=string
>
> let make (x : 'a)=(Marshal.to_string x [] : 'a marshalled);;
>
> And then do all type of operations in a type safe way on strings.

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
#

-- Damien