简体   繁体   中英

F# Comment error

I am teaching myself F# I am normally a C# programmer.

I am trying to use the (**) to make notes for myself as I go through the chapters but I am getting an error from the comment itself.

module Characters

let vowels = ['a', 'e', 'i', 'o', 'u']

printfn "Hex u0061 = '%c'" '\u0061'

(*  <------------Error is here, is 'End of file in string embedded in comment begun at or before here'
    Character Escape Sequences

    Character       Meaning
    -------------------------------
    \'              Single Quote
    \"              Double Quote
    \\              Backslash
    \b              Backspace
    \n              Newline
    \r              Carriage Return
    \t              Horisontal Tab
*)

Is this treating the comment like a string meaning I have to escape my comment?

F# trivia time! This is by design. Block comments can be nested, and strings within a block comment are tokenized like regular strings . Valid char literals count as "strings" in this case.

So these are valid block comments:

(* "embedded string, this --> *) doesn't close the comment" *)
(*  (* nested *) comment *)
(* quote considered to be in char literal '"' is ok *)

But these are not

(* "this string is not closed *)
(* " this quote --> \" is escaped inside a string *)

And as if that wasn't crazy enough, there is special treatment for operators which begin with * since (*) and the like would normally be considered the start or end of a block comment.

(* I can talk about the operator (*) without ending my comment *)

AFAIK, these are all inherited from ML (nested comments definitely are, not sure about strings).

So for your purposes, you might want to do something like this:

(*  Character       Meaning
    -------------------------------
    " \' "          Single Quote
    " \" "          Double Quote

    or

    '\''            Single Quote
    '"'             Double Quote
*)

It appears that the "Double Quote" line is the problem. If I remove that line, the error goes away. This looks like a bug in the parser, since the problem does not occur if I prefix each line with // instead of doing a block-comment. I'd suggest you send this to fsbugs@microsoft.com - if it's not already fixed for Visual Studio 2013, maybe it's not too late.

Completely unrelated: your vowels list contains a single element that is a 5-part tuple. If you want it to be a list of characters instead of a list containing one 5-part tuple, use semicolons instead of commas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM