Commenting causes a compile error (unterminated string literal in comment)

I wanted to reference a ruby snippet in a comment, like such (simplified):

(* [1,2,3].map {|n| n + 1} *)
let x = (( + ) 1) [ 1; 2; 3 ]

To my surprise the comment caused a compile error!

I found a few github issues, closed as this is expected behavior.

I’m posting here for 2 reasons:

  1. to document a workaround for my use-case
  2. to ask why this feature exists?

So, after a bit of digging, and after having understood the behavior, I realized that I should “simply” make my whole comment a string literal, like such:

(*{| [1,2,3].map {|n| n + 1} |}*)
let x = (( + ) 1) [ 1; 2; 3 ]

In my case, I could also do that but that’d be super confusing.

(* [1,2,3].map {|n| n + 1} *)
let x = (( + ) 1) [ 1; 2; 3 ]

Commenting causing a compile error is very surprising


The reason is the ability to put comment delimiters around a well-formed block of OCaml code to get a well-formed comment. For example, suppose you have

let s = "*)"

The goal is that

(* let s = "*)" *)

should be well-formed. (Do not let the syntax highlighting confuse you.)

Unfortunately, this motivation only extends to OCaml code and not to arbitrary code from any other language, hence why it breaks when you try to comment out a block of Ruby code.

By the way, you could also written your comment as

(* [1,2,3].map { |n| n + 1 } *)
1 Like

Why would we want well formed OCaml inside a generic comment?

I was under the impression that there was already a special comment syntax for code blocks.

    let incr = ( + ) 1

vscode gives a semi-working syntax highlighting + type info on mouse hover when we put these special delimiters.

Thanks, I did think about it :slight_smile:

Often, you just want to comment out a block of code in the process of developing. For example, because the compilation temporarily breaks, or because you want to test a variant. Sure, you could completely delete this block of code rather than commenting it out, and then use your favorite revision control software to later recover the original code, but that would be needlessly tedious.

So, a good programming language needs the ability to comment out whole blocks of code. Now, the question is whether standard comments should be used for this purpose or whether a special syntax should be added. For example, in C, you cannot use standard comments (/* ... */) to comment out a block code; you need to use #if 0 ... #endif instead. For OCaml, the choice was made to use standard comments.

1 Like

Which is only a problém if the language - like OCaml - doesn’t have single line comments. Yes, there has been a time, when most editors/IDEs didn’t have a “comment out región” command.

Is there a chance that OCaml ever gets these? This is one of the small papercuts that make OCaml unpleasant to use.

With single-line comments you would have to comment out one line at a time. Every line will be in the diff. With multiline comments you just need to change two lines (at most).

And that’s exactly what I want and why I do not like to see multi-line comments used for that.
The only time when multi-line comments are to be used is for multi-line prose or to make inline comments like

let f x (* x is an int *) = 2 * x

Mmm I think that’s necessary only for nested comments.

I agree it’d be nice if OCaml were to support single line comments.

I personally like to have both

No. For example, the following code has no nested comments, yet you would have a hard time enclosing it in /* ... */:

char const *regex = "^[^/]*/\\(.*\\)$";