Computer Science Canada O'Caml syntactic oddities explained |
Author: | wtd [ Tue May 17, 2005 4:47 pm ] | ||||||||||||||
Post subject: | O'Caml syntactic oddities explained | ||||||||||||||
Tuple types Tuples are critical to O'Caml, and immensely useful, but their type representation can be a bit confusing. Let's say I enter the following at the O'Caml toplevel:
Now it shows me:
But what the heck is "int * float"? Well, in this case the asterisk indicates a tuple of an int and a float. Where is this really an issue? Fortunately O'Caml infers types very well so we almost never have to worry about this. There are at least a few places where we do have to worry about it, though. "Algebraic" types are one example. Let's look at a simple binary tree definition.
Here the 'a is the generic type that can stand in for any type. The three possible forms of a binary tree are empty, a single leaf with one value, and a branch with one value and two subtrees. Each "constructor" only takes one argument which is a tuple. Seeing this is action:
The other places we see this syntax is with exceptions. Let's say we have a bank_account class.
Now, what if I want to raise an exception when a bad withdrawal request is made? I could create a simple Bad_withdrawal exception, but let's say I want to pass information on the amount that they tried to withdraw and how much they have available. First, let's create the exception. It has to hold two pieces of info.
And the new class.
|