Why does Ocaml have block comments(*…*) but no line comments? I have my suspicions why but I’d like to hear what the experts have to say…
Because (O)Caml started with block comments and it is quite hard to retrofit line comments into the current grammar.
I guess I was way off with my suspicions… I thought it might have to do with let in bindings and implied let in bindings that made it hard to determine where the end of the line is.
I remember seeing talks of adding line comments starting with
(*) since it’s already an illegal token in the langauge right now, but I guess having to write the multiplication operator like
( * ) is already bad enough by itself and it would just lead to confusing error messages for beginners.
You are thinking of https://github.com/ocaml/ocaml/pull/671 .
(*) only raises a warning and is legal currently.
Many of us would like to have single-line comments. It’s just that all the familiar prefixes for single-line comments (e.g.
;) already have a different meaning in OCaml, so some existing code would break. What’s left (e.g.
(*)) is not very attractive.
I used think that I needed single line comments in OCaml (or any other language for that matter), but then I realized that since I was using VIM there’s a nice plugin which makes it really easy to toggle comments for single or multiple lines or any kind of selection I believe.
I know it’s not a proper solution, but if you happen to use VIM you might want to check it out.
Is it simply a question of finding a pleasant comment character sequence?
Wouldn’t backwards compatibility also be an issue? Most comment prefixes I can think of are valid infix operators.
(*) was being proposed for a time, since it isn’t a valid comment character.
One thought I’ve had is using a Unicode symbol, but that would require that OCaml make a definitive shift to Unicode as its character set for source code, and there would be people who would feel irritated by a choice that required editor support to enter.
For what it’s worth, the
(*) is a line comment in last release version of MLTON, but you have to explicitly enable it using an annotation
But there is another possibility. We already have
#load which are interpreted as instructions to interpreter.
So why not add add
#comment) to stands for a line comment?
That’s already a valid pattern for matching subtypes of a polymorphic variant:
type co = [ `A | `B ] let x : [ `A | `B | `C ] = `A let _ = match x with | `C -> "c" | #co -> "ab"
I see. So it will not work.