Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] assertions or exceptions?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] Unboxing options, was RE: assertions or exceptions?
On Thu, 15 Jul 2004 16:49:49 -0400, John Carr <jfc@mit.edu> wrote:
> Is there valid code (no Obj.magic) that cares that (Some None) and
> (None) are both represented by the same bit pattern?

module A =
  struct
    type t = string option
    let default = ref ""
    let set_default x = default := x
    let get_default = !default
    let create x = Some x
    let create_default () = None
    let get_value = function Some x -> x | None -> !default
  end : sig
    type t
    val set_default : string -> unit
    val get_default : unit -> string
    val create : string -> t
    val create_default : unit -> t
    val get_value : t -> string
  end

(* this comes from somewhere else... perhaps it reads a configuration
   file, and uses None if the value isn't available, and Some
(A.get_default ()) if
   it's a specific value meaning "the current default value". *)
val get_attribute : string -> A.t option

...
    match get_attribute "pants" with
      | Some x -> (* called when the value was in the file *)
      | None -> (* called when either the value wasn't in the file
*or* it's the default,
                   represented as Some None *)

In this case, the user of the module A doesn't even know that type t
is represented using an option type underneath--the user just knows
that the value could either be the default
value or a specific value.  The writer of A could of course choose to
make his own special type:

type t = Default | Value of string

But it's rather silly to say that "if you think someone might put your
type into an option, you should probably make your own different type
which is identical to option but doesn't do magic behind the scenes."

John.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners