From: David Mentre <David.Mentre@inria.fr>
To: leary@nwlink.com
Cc: caml <caml-list@inria.fr>
Subject: Re: [Caml-list] enums in OCaml?
Date: 13 Jun 2001 09:37:12 +0200 [thread overview]
Message-ID: <qtl7kyg3k5z.fsf@pochi.inria.fr> (raw)
In-Reply-To: <20010612192126.B24440@jean>
leary@nwlink.com writes:
> What's the right way to make something like C's enums in OCaml. I found a
> couple references to sum types online and in the manual and caml light
> tutorial, but none of it was really what I'm looking for: alias a number
> with a name, hopefully conveniently.
sum type and pattern matching is really what you are looking for:
# type my_enum = Case1 | Case2 | Case3;;
type my_enum = Case1 | Case2 | Case3
# let num c = match c with Case1 -> 1 | Case2 -> 2 | Case3 ->3 ;;
val num : my_enum -> int = <fun>
# num Case3;;
- : int = 3
Usually, you do not need to call the num function, you can use the
symbolic name directly in your code. And at the opposite of C enum, if a
case is forgotten, the compiler warns you (usefull when you extend your
enum):
# let faulty_num c = match c with Case1 -> 1 | Case2 -> 2;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Case3
val faulty_num : my_enum -> int = <fun>
Best regards,
d.
--
David.Mentre@inria.fr -- http://www.irisa.fr/prive/dmentre/
Opinions expressed here are only mine.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-06-13 9:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-13 2:21 leary
2001-06-13 7:37 ` David Mentre [this message]
2001-06-13 8:20 ` leary
[not found] ` <200106130751.f5D7pbL11758@ionie.inria.fr>
2001-06-13 8:18 ` leary
2001-06-13 8:46 ` David Mentre
2001-06-14 8:15 ` leary
2001-06-14 8:41 ` Chris Hecker
2001-06-14 8:53 ` Jacques Garrigue
2001-06-14 9:12 ` Francisco Valverde Albacete
2001-06-14 13:04 ` Chris Quinn
2001-06-13 20:50 leary
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=qtl7kyg3k5z.fsf@pochi.inria.fr \
--to=david.mentre@inria.fr \
--cc=caml-list@inria.fr \
--cc=leary@nwlink.com \
/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