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] Frustrated Beginner
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-12-23 (12:17)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Frustrated Beginner
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)



Richard Jones.
Merjis Ltd. - improving website return on investment
MOD_CAML lets you run type-safe Objective CAML programs inside the Apache

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