Re:Unix system programming in OCaml (2014)
by theaeolist on Thursday July 12, 2018 @07:23AM
(#17513527)
OCaml has no guaranteed order of evaluation, which is not what you want with (;). It needs to be part of the language as special syntax.
Re:Unix system programming in OCaml (2014)
by LeonidasXIV on Thursday July 12, 2018 @10:26AM
(#17514913)
The signature of `;` where it an operator is more like
(;) : unit -> 'a -> 'a
And you can see it as a kind of sequencing operator, since it returns the value of the right hand side unchanged.
(;) : unit -> unit -> unit
Of course, the reality is more complicated than this, because it can be used at the end of an expression, where it behaves like a postfix operator of type 'a -> 'a. Let's take your example:
you can actually do This doesn't just work for imperative stuff, like the operator theory of the semicolon, it will work with this example.My understanding is that this is due to the fact that optional semicolons are allowed at the end of statements and close the current expression.
So we're closing the first expression (None) and then closing the second expression (the inner match). The double semicolon is something else altogether.
Though it is worth noting that this example is straying from idiomatic OCaml and we can avoid this altogether with better matching:
Both the native optimizing compiler and BuckleScript are able to do what you would expect with this: the matched tuple is optimized away.Your second unfixed example actually causes a syntax error. There is an if expression in OCaml and an if statement. This is particularly unfortunate, because the let cache_file = function | file when file_exists file -> printf "downloading file\n"; download file | _ -> printf "file already exists\n" first semicolon within an if expression turns the whole thing into an if statement, and an if statement can have no else clause. The else clause is then without an if statement, because we've triggered the imperative if statement. This is a syntax error.
were we to define an operator
then we could replace that semicolon with an >>> and things behave as expected. But it's really that if can be both a statement and an expression. For this reason I prefer using matches in general.