caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: William Lovas <wlovas@stwing.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] exception Failure and failwith
Date: Tue, 21 Jun 2005 14:55:06 -0400	[thread overview]
Message-ID: <20050621185506.GA7604@force.stwing.upenn.edu> (raw)
In-Reply-To: <20050621113440.GA3794@yquem.inria.fr>

On Tue, Jun 21, 2005 at 01:34:40PM +0200, Luc Maranget wrote:
> > It's what I was afraid of.
> > I got it. Anyway let me make a consideration: It would be nice to have a 
> > way (a fake module named Core,for example) to access the core types, 
> > exception and definitions in case someone shadows the original one.
> 
> Why not, but frankly, I am not sure this can be done easily. Besides,
> this would potentially break user code, for users who happen to have a
> module named 'Core'.

In fact, one could easily build such a "Core" module as a user, following
Julien's advice:

> > exception Prim_Failure = Failure
> > 
> > to get rid of any further shadowing.

Just take this to the next level with something like:

    module Core =
      struct
        exception Failure = Failure
        type 'a opt = 'a option = None | Some of 'a
        type 'a option = 'a opt
        (* ... *)
      end

(Note the minor contortion: we have to introduce a fake name 'a opt in
order to avoid creating a cyclic type abbreviation, since data type
definitions are automatically recursive.  But of course, you can skip the
second step if you don't care about having a type called 'a Core.option)

While tedious, it's the sort of thing you could add to as you discover new
reasons to do so.  This Core module would allow interactions like the
following situation, which might conceivably arise due to careless
shadowing:

    (* function defined over primitive 'a option *)
    let valOf = function
        | None -> failwith "valOf"
        | Some x -> x;;

    (* evil shadowing *)
    type 'a option = None of 'a | Some of int
    exception Failure of bool;;

    try valOf None with Failure s -> s;;          (* doesn't typecheck *)
    try valOf Core.None with Failure s -> s;;     (* doesn't catch Failure *)
    try valOf Core.None with Core.Failure s -> s;;    (* works! *)

cheers,
William


  parent reply	other threads:[~2005-06-21 18:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-21  8:34 Luca Pascali
2005-06-21  9:07 ` [Caml-list] " Luc Maranget
2005-06-21 11:15   ` Luca Pascali
2005-06-21 11:34     ` Luc Maranget
2005-06-21 18:23       ` sejourne_kevin
2005-06-21 18:55       ` William Lovas [this message]
2005-06-23  8:04         ` Luca Pascali
2005-06-21  9:30 ` Julien Signoles

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=20050621185506.GA7604@force.stwing.upenn.edu \
    --to=wlovas@stwing.org \
    --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;
as well as URLs for NNTP newsgroup(s).