From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA14560; Thu, 15 Jul 2004 23:15:17 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA14977 for ; Thu, 15 Jul 2004 23:15:16 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.200]) by nez-perce.inria.fr (8.12.10/8.12.10) with SMTP id i6FLFEEV016788 for ; Thu, 15 Jul 2004 23:15:15 +0200 Received: by mproxy.gmail.com with SMTP id d78so438018rnf for ; Thu, 15 Jul 2004 14:15:14 -0700 (PDT) Received: by 10.38.207.42 with SMTP id e42mr189901rng; Thu, 15 Jul 2004 14:15:14 -0700 (PDT) Message-ID: Date: Thu, 15 Jul 2004 17:15:14 -0400 From: John Prevost To: Ocaml Mailing List Subject: Re: [Caml-list] Unboxing options, was RE: assertions or exceptions? In-Reply-To: <200407152049.i6FKnnSd004687@psi-phi.mit.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <200407152049.i6FKnnSd004687@psi-phi.mit.edu> X-Miltered: at nez-perce with ID 40F6F3E2.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; prevost:01 prevost:01 caml-list:01 unboxing:01 2004:99 0400,:01 jfc:01 struct:01 val:01 val:01 pants:99 sig:01 assertions:01 match:02 silly:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 = 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