caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@gmail.com>
To: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Unboxing options, was RE: assertions or exceptions?
Date: Thu, 15 Jul 2004 17:15:14 -0400	[thread overview]
Message-ID: <d849ad2a04071514153cd24c26@mail.gmail.com> (raw)
In-Reply-To: <200407152049.i6FKnnSd004687@psi-phi.mit.edu>

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


  reply	other threads:[~2004-07-15 21:15 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15  8:03 [Caml-list] " Radu Grigore
2004-07-15 10:18 ` Richard Jones
2004-07-15 10:28   ` Daniel Andor
2004-07-15 12:49   ` Radu Grigore
2004-07-15 13:33     ` Richard Jones
2004-07-15 13:58       ` Radu Grigore
2004-07-16 18:53         ` Aleksey Nogin
2004-07-17  2:55           ` John Prevost
2004-07-17 14:24             ` David MENTRE
2004-07-15 12:35 ` Jon Harrop
2004-07-15 13:45   ` Radu Grigore
2004-07-15 14:33     ` Jon Harrop
2004-07-15 15:05       ` Radu Grigore
2004-07-15 16:24     ` skaller
2004-07-15 15:38 ` [Caml-list] Unboxing options, was " Brian Hurt
2004-07-15 16:25   ` John Hughes
2004-07-15 17:00     ` Brian Hurt
2004-07-15 17:20   ` John Prevost
2004-07-15 19:14     ` Radu Grigore
2004-07-15 19:56     ` John Carr
2004-07-15 20:48       ` Brian Hurt
2004-07-15 20:49         ` John Carr
2004-07-15 21:15           ` John Prevost [this message]
2004-07-15 21:15           ` Karl Zilles
2004-07-15 21:26           ` Brian Hurt
2004-07-15 21:04       ` John Prevost
2004-07-15 21:17     ` skaller
2004-07-15 21:35       ` Brian Hurt
2004-07-15 21:51         ` skaller
2004-07-15 21:42       ` skaller
2004-07-16  0:35     ` Jacques GARRIGUE
2004-07-16  1:03       ` John Prevost
2004-07-16  2:00         ` Jacques GARRIGUE
2004-07-16 16:40         ` Xavier Leroy
2004-07-19  8:58           ` Damien Doligez

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=d849ad2a04071514153cd24c26@mail.gmail.com \
    --to=j.prevost@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;
as well as URLs for NNTP newsgroup(s).