English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
exception Failure and failwith
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-06-21 (18:55)
From: William Lovas <wlovas@s...>
Subject: Re: [Caml-list] exception Failure and failwith
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 =
        exception Failure = Failure
        type 'a opt = 'a option = None | Some of 'a
        type 'a option = 'a opt
        (* ... *)

(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

    (* 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! *)