Why no line comments


#1

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…


#2

Because (O)Caml started with block comments and it is quite hard to retrofit line comments into the current grammar.


#3

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.


#4

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.


#5

You are thinking of https://github.com/ocaml/ocaml/pull/671 .
Note that (*) only raises a warning and is legal currently.


#6

Many of us would like to have single-line comments. It’s just that all the familiar prefixes for single-line comments (e.g. // and # and ;) already have a different meaning in OCaml, so some existing code would break. What’s left (e.g. (*)) is not very attractive.


#7

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.


#8

Is it simply a question of finding a pleasant comment character sequence?


#9

Wouldn’t backwards compatibility also be an issue? Most comment prefixes I can think of are valid infix operators.


#10

That’s why (*) 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.


#11

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 #use and #load which are interpreted as instructions to interpreter.

So why not add add #c (#co, #comment) to stands for a line comment?


#12

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"

#13

I see. So it will not work.