Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Some/None
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] Some/None
Oliver Bandel schrieb am Mittwoch, den 24. April 2002:
> I have not found any description in the
> Ref-Manual (for 3.01).

This type is implicitly defined in the "Pervasives"-module as follows:

  type 'a option = None | Some of 'a

As the name indicates, you can use it to describe optional values, i.e.
you either have "Some value" or you have "None". It has a similar purpose
as NULL-pointers in C, but with the difference that it is perfectly safe
to handle.

When you want to "see" what an optional value looks like, you use
pattern-matching:

  match maybe_data with
  | Some data ->  (* do something with "data" *)
  | None ->       (* handle case when no data available *)

There is no way to accidently access "maybe_data" as if it were "Some
data" even though it is actually "None" - a very common programming
mistake in C.

The option type is really just an ordinary algebraic datatype, which is
useful enough to deserve being defined in the standard library.

LG,
Markus

-- 
Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
-------------------
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