From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] bizarre type
Date: Sun, 03 Jul 2005 21:37:37 +0900 (JST) [thread overview]
Message-ID: <20050703.213737.63742895.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <d545e6e677699d1a8d6420206aeeac84@inria.fr>
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
prev parent reply other threads:[~2005-07-03 12:37 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-06-30 15:48 Julien Verlaguet
2005-06-30 16:49 ` [Caml-list] " Andreas Rossberg
2005-06-30 16:58 ` Julien Verlaguet
2005-06-30 17:16 ` Stephane Glondu
2005-06-30 17:24 ` Andreas Rossberg
2005-06-30 18:30 ` Julien Verlaguet
2005-06-30 19:37 ` Andreas Rossberg
2005-06-30 21:42 ` Julien Verlaguet
2005-06-30 23:57 ` Jacques Garrigue
2005-07-03 11:42 ` Damien Doligez
2005-07-03 12:37 ` Jacques Garrigue [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050703.213737.63742895.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox