I am following this draw_string_in_box example in Developing Applications with OCaml
The sample code given is as follows:
let draw_string_in_box pos str bcf col =
let (w, h) = Graphics.text_size str in
let ty = bcf.y + (bcf.h-h)/2 in
( match pos with
Center -> Graphics.moveto (bcf.x + (bcf.w-w)/2) ty
| Right -> let tx = bcf.x + bcf.w - w - bcf.bw - 1 in
Graphics.moveto tx ty
| Left -> let tx = bcf.x + bcf.bw + 1 in Graphics.moveto tx ty );
Graphics.set_color col;
Graphics.draw_string str;;
If I remove the parentheses around the “match” part, the code won’t work (nothing gets printed). Any idea why?
And more generally, when should I put parenthese around code bits like this?
Thanks.
One way to look at it is that after the arrow
->of amatchstatement you can have a sequence of expressions (separated by;). Without the parentheses, the following expressions look like they’re part of the last case of thematch.You can also have a sequence of expressions (separated by
;) afterlet. With the parentheses, the following expressions look like they’re part of thelet, which is what you want.Personally I avoid using
;. That’s how I deal with this problem! Otherwise you have to figure the expression sequence goes with the innermost construct that takes a sequence.