26

I'm learning ML, with the SML/NJ dialect. What I'm trying to figure out is if there is a line comment operator. I found the block comment operator, (* ... *), but I really miss line comments.

Suggestions? Or am I just stuck with block comments?

icco
  • 3,064
  • 4
  • 35
  • 49

3 Answers3

34

You're stuck with block comments.

On the other hand, block comments can be nested: (* (* *) still comment here *)

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
  • This makes me sad, but oh well. And the nested comments are cool, thanks. – icco Jan 09 '10 at 17:33
  • 2
    For the record, that depends on your compiler/interactive environment. According to the spec, a comment may not contain the substring "*)", so you should avoid it even if some implementations are lenient. – Nicholas Wilson Apr 22 '10 at 16:29
  • 3
    @Nicholas Wilson: you are right, I got things mixed up with OCaml (http://caml.inria.fr/pub/docs/manual-ocaml/lex.html). – Victor Nicollet Apr 28 '10 at 11:11
  • 4
    @NicholasWilson, just discovered this old answer. For the record, nested comments are explicitly allowed by the SML language spec ("Sec. 2.3: A comment is any character sequence within comment brackets `(*` `*)` in which comment brackets are properly nested"). All implementations support them correctly, and always have. Also, FWIW, both MLton and SML/NJ have recently implemented an old proposal for allowing line comments via `(*)`, although that's not shipping yet. – Andreas Rossberg Jul 01 '15 at 19:14
  • OK, I stand corrected -- but I think I did look it up five years ago, maybe I had an older spec from the library in my hand. – Nicholas Wilson Jul 02 '15 at 09:48
4

There is a RFC for line comments, which proposes a hashmark followed by a whitespace.

Adam Howell
  • 415
  • 1
  • 11
  • 24
  • 5
    I don't know who wrote up with that proposal, but it's incompatible with SML: `# a` already is valid syntax that is used in existing code. The proposal that has been implemented in some compilers is using `(*)` to start a line comment. – Andreas Rossberg Aug 31 '17 at 08:19
  • 3
    I don't know why I am being downvoted for mentioning an RFC. I am just providing more information, not telling you how to vote. – Adam Howell Sep 01 '17 at 16:03
1

Single-line comments now ship in both MLton and SML/NJ, as long as you enable sML ("Successor ML") extensions (sml -Cparser.succ-ml=true for SML/NJ).

Here's a concrete example. In the definition below, the value 1 is ignored, and the definition of a is taken from the next line (2) instead. (Below = denotes a continuation line, and please ignore the broken syntax highlighting.)

$ sml -Cparser.succ-ml=true
- val a = (*) 1
=   2;;
val a = 2 : int

See https://github.com/SMLFamily/Successor-ML/wiki/Summary-of-proposed-changes for more about sML.

Clément
  • 12,299
  • 15
  • 75
  • 115