Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] Re: meaning of ;; in source code

Expand Messages
  • Lukasz Stafiniak
    ... Without the ;; , the parser had to assume that add was just another argument to add_group above. I use the following style, without ;; : let g =
    Message 1 of 25 , Sep 13, 2010
    • 0 Attachment
      On Mon, Sep 13, 2010 at 11:15 PM, Gaius Hammond <gaius@...> wrote:
      > OK, got it - so the code I posted *shouldn't* have worked because it
      > had the bare "add options..." but it was allowed through by the parser
      > because of the ;;.
      >

      Without the ";;", the parser had to assume that "add" was just another
      argument to "add_group" above. I use the following style, without
      ";;":

      let g = add_group options...
      let _ = add options...

      "let" means that you define a computation, and "_" means that you are
      not interested in the result of the computation, only in the side
      effects.
    • David House
      ... This is not quite true. The ;; is used to separate expressions at the top level of your modules, but it is not needed when the next thing is a let, module,
      Message 2 of 25 , Sep 16, 2010
      • 0 Attachment
        On 13 September 2010 19:07, Gaius Hammond <gaius@...> wrote:
        > Someone says "The ;; separator is only needed when signaling the
        > toplevel that it should take what you've given it and interpret it"
        > which is what I thought but here:
        >
        > http://mancoosi.org/~abate/extlib-optparse-module-options-parsing-made-easy-and-clean

        This is not quite true. The ;; is used to separate expressions at the
        top level of your modules, but it is not needed when the next thing is
        a let, module, type etc. expression. Only if followed by "raw code"
        will you need end a statement with a ;;.

        For example, this works:

        let foo = ...
        let bar = ...
        let main () = ...

        But this doesn't:

        let foo = ...
        let bar = ...
        let main () = ...
        main ()

        Because the "let main () = ..." is followed by "raw code", the call
        "main ()". You'd need to write this as:

        let foo = ...
        let bar = ...
        let main () = ...;;
        main ()

        Or the following alternative, which is quite idiomatic:

        let foo = ...
        let bar = ...
        let main () = ...
        let () = main ()

        (Presuming that main returns unit.)

        The precise rules can be found here:
        http://caml.inria.fr/pub/docs/manual-ocaml/manual019.html
        (Basically, ;; is needed before anything that isn't a let, external,
        type, exception, class, class type, module, module type, open or
        include statement.)

        Hope that helps,
        -David
      • Lukasz Stafiniak
        ... [...] ... For bigger amounts of testing code, I slightly prefer the idiom: let _ = test_foo bar_arg baz_arg because it allows you to comment out the code
        Message 3 of 25 , Sep 16, 2010
        • 0 Attachment
          On Thu, Sep 16, 2010 at 2:07 PM, David House <dmhouse@...> wrote:
          >
          > This is not quite true. The ;; is used to separate expressions at the
          > top level of your modules, but it is not needed when the next thing is
          > a let, module, type etc. expression. Only if followed by "raw code"
          > will you need end a statement with a ;;.

          [...]

          > Or the following alternative, which is quite idiomatic:
          >
          > let foo = ...
          > let bar = ...
          > let main () = ...
          > let () = main ()
          >
          > (Presuming that main returns unit.)

          For bigger amounts of testing code, I slightly prefer the idiom:

          let _ = test_foo bar_arg baz_arg

          because it allows you to "comment out" the code execution while still
          having the compiler typecheck the code:

          let _ = fun () -> test_foo bar_arg baz_arg

          or the following, which unfortunately works only over camlp4, at least
          in OCaml 3.11.2:

          let _ () = test_foo bar_arg baz_arg
        • Sebastien Mondet
          ... just 2 c.: the one with () is safer because the _ can swallow typing errors (e.g. if you forget one argument). ... [Non-text portions of this message
          Message 4 of 25 , Sep 16, 2010
          • 0 Attachment
            On Thu, Sep 16, 2010 at 14:30, Lukasz Stafiniak <lukstafi@...> wrote:

            > On Thu, Sep 16, 2010 at 2:07 PM, David House <dmhouse@...> wrote:
            > >
            > > This is not quite true. The ;; is used to separate expressions at the
            > > top level of your modules, but it is not needed when the next thing is
            > > a let, module, type etc. expression. Only if followed by "raw code"
            > > will you need end a statement with a ;;.
            >
            > [...]
            >
            > > Or the following alternative, which is quite idiomatic:
            > >
            > > let foo = ...
            > > let bar = ...
            > > let main () = ...
            > > let () = main ()
            > >
            > > (Presuming that main returns unit.)
            >
            > For bigger amounts of testing code, I slightly prefer the idiom:
            >
            > let _ = test_foo bar_arg baz_arg
            >
            > because it allows you to "comment out" the code execution while still
            > having the compiler typecheck the code:
            >
            > let _ = fun () -> test_foo bar_arg baz_arg
            >
            >
            just 2 c.:
            the one with "()" is safer because the "_" can swallow typing errors (e.g.
            if you forget one argument).





            > or the following, which unfortunately works only over camlp4, at least
            > in OCaml 3.11.2:
            >
            > let _ () = test_foo bar_arg baz_arg
            >
            >
            > ------------------------------------
            >
            > Archives up to December 31, 2009 are also downloadable at
            > http://www.connettivo.net/cntprojects/ocaml_beginners/
            > The archives of the very official ocaml list (the seniors' one) can be
            > found at http://caml.inria.fr
            > Attachments are banned and you're asked to be polite, avoid flames
            > etc.Yahoo! Groups Links
            >
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Gaius Hammond
            Thanks everyone :-) G ... [Non-text portions of this message have been removed]
            Message 5 of 25 , Sep 16, 2010
            • 0 Attachment
              Thanks everyone :-)




              G






              On 16 Sep 2010, at 13:07, David House wrote:

              > On 13 September 2010 19:07, Gaius Hammond <gaius@...> wrote:
              > > Someone says "The ;; separator is only needed when signaling the
              > > toplevel that it should take what you've given it and interpret it"
              > > which is what I thought but here:
              > >
              > > http://mancoosi.org/~abate/extlib-optparse-module-options-parsing-made-easy-and-clean
              >
              > This is not quite true. The ;; is used to separate expressions at the
              > top level of your modules, but it is not needed when the next thing is
              > a let, module, type etc. expression. Only if followed by "raw code"
              > will you need end a statement with a ;;.
              >
              > For example, this works:
              >
              > let foo = ...
              > let bar = ...
              > let main () = ...
              >
              > But this doesn't:
              >
              > let foo = ...
              > let bar = ...
              > let main () = ...
              > main ()
              >
              > Because the "let main () = ..." is followed by "raw code", the call
              > "main ()". You'd need to write this as:
              >
              > let foo = ...
              > let bar = ...
              > let main () = ...;;
              > main ()
              >
              > Or the following alternative, which is quite idiomatic:
              >
              > let foo = ...
              > let bar = ...
              > let main () = ...
              > let () = main ()
              >
              > (Presuming that main returns unit.)
              >
              > The precise rules can be found here:
              > http://caml.inria.fr/pub/docs/manual-ocaml/manual019.html
              > (Basically, ;; is needed before anything that isn't a let, external,
              > type, exception, class, class type, module, module type, open or
              > include statement.)
              >
              > Hope that helps,
              > -David
              >
              >



              [Non-text portions of this message have been removed]
            • Lukasz Stafiniak
              Another syntax-semantics point to contemplate (relates to your initial confusion). You can replace blah bar baz; foo bar by let () = blah bar baz in foo bar
              Message 6 of 25 , Sep 16, 2010
              • 0 Attachment
                Another syntax-semantics point to contemplate (relates to your initial
                confusion). You can replace

                blah bar baz;
                foo bar

                by

                let () = blah bar baz in
                foo bar


                This can help with some syntactic quirks if you don't find parentheses
                all that pretty. E.g.

                let () = match foo () with None -> ()
                | Some report -> Printf.printf "report=%d%!" report in
                blah ()

                On Thu, Sep 16, 2010 at 6:55 PM, Gaius Hammond <gaius@...> wrote:
                > Thanks everyone :-)
              Your message has been successfully submitted and would be delivered to recipients shortly.