On Wed, Mar 21, 2007 at 12:34:53PM +0000, Joel Reymont wrote:
> Folks,
>
> Is there any drawback to using match instead of "if then else"?
>
> I have a sizeable chunk of code to run in the "then" clause and
> rather than use a begin ... begin I would prefer to just use match.
If it's really just 'if ... then ... else ...' versus a match clause
with two cases, then it's unlikely to make any difference.
If the match clause has many cases, then in theory it can be
optimised better, so for example:
match i with
| 0 -> ...
| 1 -> ...
(* ... *)
| 100 -> ...
is potentially better than a lot of 'if ... else if ... else if ...'.
Whether the compiler actual does the right optimisations is another
matter. Apparently with string matching it does not (according to a
recent mail on caml-list).
Note that instead of begin ... end, you can use parentheses. I prefer
them, but that's a matter of style.
if test then (
...
)
Rich.
--
Richard Jones
Red Hat
.