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 NAA07681; Tue, 23 Dec 2003 13:17:01 +0100 (MET) 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 NAA07829 for ; Tue, 23 Dec 2003 13:17:00 +0100 (MET) Received: from aomori.annexia.org (annexia.force9.co.uk [212.56.101.183]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBNCGxv09347 for ; Tue, 23 Dec 2003 13:16:59 +0100 (MET) Received: from rich by aomori.annexia.org with local (Exim 3.36 #1 (Debian)) id 1AYlT8-0007qi-00; Tue, 23 Dec 2003 12:16:58 +0000 Date: Tue, 23 Dec 2003 12:16:57 +0000 Cc: Tyler Eaves , caml-list@inria.fr Subject: Re: [Caml-list] Frustrated Beginner Message-ID: <20031223121657.GB29933@redhat.com> References: <1072152186.59938.30.camel@tylere> <29911.24.6.3.124.1072153159.squirrel@www.cs.fiu.edu> <1072152186.59938.30.camel@tylere> <20031223053451.GB1145@swordfish> <20031223012223.W47896@bowser.eecs.harvard.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20031223012223.W47896@bowser.eecs.harvard.edu> User-Agent: Mutt/1.5.4i From: Richard Jones X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 tucker:01 struct:01 foo:01 enum:01 val:01 val:01 unspecified:01 enforced:01 pointers:01 scalars:01 char:01 char:01 freshmeat:01 type-safe:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, Dec 23, 2003 at 01:31:42AM -0500, Michael Jeffrey Tucker wrote: > struct foo_type { > enum {Some, None} opt; > value_t val; > }; > > If the opt field of the structure is set to Some, then you are free to > look at the val field. If it's none, then the value in the val field is > unspecified and should be ignored (of course this is more stringently > enforced in OCaml). In OCaml an "option type" variable has either the > value "Some xxx" or "None" where xxx is a value. So, a "string option" > variable might have the value Some "hi", Some "qwery" or None. If you > pattern match on a variable of this type, as in: I like to think of the 'string option' type as being a bit like two concepts in more familiar languages: (1) NULL pointers in C, or (2) Scalars in Perl, which can be 'undef'. For example: C OCaml Perl char *str = "hello"; Some "hello" $s = "hello" char *str = NULL; None $s = undef The basic 'string' type in OCaml can never have a NULL value. Sure, it can be an empty string ("") but this is different from it having no value at all. BTW, 'string option' is very easily defined in OCaml as: type 'a option = None | Some of 'a which naturally brings us to the subject of polymorphic types. The above definition defines a whole family of types, which can be thought of like a wildcard '* option' where '*' is any existing type. eg: Expression Type Some 1 int option Some 'c' char option Some [1] int list option Some (Some 1) int option option Some [] 'a list option (tricky one, this) etc. Rich. -- Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj Merjis Ltd. http://www.merjis.com/ - improving website return on investment MOD_CAML lets you run type-safe Objective CAML programs inside the Apache webserver. http://www.merjis.com/developers/mod_caml/ ------------------- 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