From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Magic with fun (type t) ... ?
Date: Thu, 27 Jan 2011 00:11:31 +0100 [thread overview]
Message-ID: <AANLkTi=ZmJ6iexTF1zjwKNANj9pShkPvzfLP02_92wWh@mail.gmail.com> (raw)
In-Reply-To: <20110126221756.GA5907@vaio.jimpryor.net>
[-- Attachment #1: Type: text/plain, Size: 1873 bytes --]
Local modules are usually "safe" in the sense that the type system checks
that the local types do not escape the whole expression.
# let test =
let module Local = struct type t = Foo end
in Local.Foo;;
Error: This `let module' expression has type Local.t
In this type, the locally bound module name Local escapes its scope
However, exceptions do not create a local type, they instead augment the
global open-ended "exn" type, so the type-checker won't detect the leak.
Finally, you don't want to disallow local exception declarations, as they're
very useful in various situations -- simply moving an existing module to a
local scope.
The use of the new (type t) construct to declare local polymorphic
exceptions is also useful in some cases. See for example:
http://ocaml.janestreet.com/?q=node/18#comment-190
(I agree that the type regression when escaping is quite weird and should be
avoided if possible.)
On Wed, Jan 26, 2011 at 11:17 PM, Jim Pryor
<lists+caml@jimpryor.net<lists%2Bcaml@jimpryor.net>
> wrote:
> I expect this is bad coding style, and should not be relied on. However,
> I was surprised at the behavior, and wondered whether it was intended:
>
> # let f = fun (type t) x ->
> let module M = struct exception E of t end in
> M.E x;;
> val f : 'a -> exn = <fun>
>
> f is polymorphic, as we expect:
>
> # f 1;;
> - : exn = E 1
> # f "s";;
> - : exn = E "s"
>
> But now notice:
>
> # f ();;
> - : exn = E 0
> # f None;;
> - : exn = E 0
>
> It appears that non-heap values are always getting magicked into ints.
>
> Has this been noted before?
>
> --
> Jim Pryor
> profjimm@jimpryor.net
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
[-- Attachment #2: Type: text/html, Size: 2898 bytes --]
prev parent reply other threads:[~2011-01-26 23:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-01-26 22:17 Jim Pryor
2011-01-26 22:33 ` David Allsopp
2011-01-27 6:13 ` Alain Frisch
2011-01-27 23:56 ` Guillaume Yziquel
2011-01-26 22:34 ` Jim Pryor
2011-01-26 23:07 ` Guillaume Yziquel
2011-01-26 23:11 ` Gabriel Scherer [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='AANLkTi=ZmJ6iexTF1zjwKNANj9pShkPvzfLP02_92wWh@mail.gmail.com' \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@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