When do you put double semicolons in F#?

前端 未结 9 1134
孤独总比滥情好
孤独总比滥情好 2021-01-01 09:39

This is a stupid question. I\'ve been reading a couple books on F# and can\'t find anything that explains when you put ;; after a statement, nor can I find a pattern in the

相关标签:
9条回答
  • 2021-01-01 10:19

    In F#, the only place ;; is required is to end expressions in the interactive mode.

    ;; is left over from the transition from OCaml, where in turn it is left over from Caml Light. Originally ;; was used to end top-level "phrases"--that is, let, type, etc. OCaml made ;; optional since the typical module consists of a series of let statements with maybe one statement at the end to call the main function. If you deviate from this pattern, you need to separate the statements with ;;. Unfortunately, in OCaml, when ;; is optional versus required is hard to learn.

    However, F# introduces two relevant modifications to OCaml syntax: indentation and do. Top-level statements have to go inside a do block, and indentation is required for blocks, so F# always knows that each top-level statement begin with do and an indent and ends with an outdent. No more ;; required.

    Overall, all you need to know is that [O']Caml's syntax sucks, and F# fixes a lot of its problems, but maintains a lot of confusing backward compatibility. (I believe that F# can still compile a lot of OCaml code.)

    Note: This answer was based on my experience with OCaml and the link Adam Gent posted (which is unfortunately not very enlightening unless you know OCaml).

    0 讨论(0)
  • 2021-01-01 10:24

    In the non-interactive F# code that's not supposed to be compatible with OCaml, you shouldn't need to ever need double semicolon. In the OCaml compatible mode, you would use it at the end of a top-level function declaration (In the recent versions, you can switch to this mode by using files with .ml extension or by adding #light "off" to the top).

    If you're using the command-line fsi.exe tool or F# Interactive in Visual Studio then you'd use ;; to end the current input for F#.

    When I'm posting code samples here at StackOverflow (and in the code samples from my book), I use ;; in the listing when I also want to show the result of evaluating the expression in F# interactive:

    • Listing from F# interactive

      > "Hello" + " world!";;
      val it : string = "Hello world!"
      > 1 + 2;;
      val it : int = 3
      
    • Standard F# source code

      let n = 1 + 2
      printf "Hello world!"
      

    Sometimes it is also useful to show the output as part of the listing, so I find this notation quite useful, but I never explained it anywhere, so it's great that you asked!

    0 讨论(0)
  • 2021-01-01 10:30

    The double semicolon most likely comes from OCaml since that is what the language is based on. See link text

    Basically its for historical purposes and you need it for the evaluator (repl) if you use it.

    0 讨论(0)
提交回复
热议问题