Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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 <> 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 =
    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

(* 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."


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: