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

Function Arguments

Expand Messages
  • Schell
    Hi guys, I was just wondering if there is an equivalent language feature to Actionscript 3 s ... args in OCaml. I m trying to declare a function that takes
    Message 1 of 4 , Aug 30, 2009
    • 0 Attachment
      Hi guys, I was just wondering if there is an equivalent language feature to
      Actionscript 3's "... args" in OCaml. I'm trying to declare a function that
      takes an unknown number of arguments and runs through them, and better yet,
      gives them all to fprintf. Here's an example:

      let trace message =
      let file = "/Library/Application Support/Commandline/trace.log" in
      let oc = open_out file in
      fprintf oc "%s\n" message;
      close_out oc;
      ;;

      I'd like to just have trace wrap fprintf with my file info and relay all the
      args to fprintf. Is this possible?

      --
      Schell Scivally
      efsubenovex@...
      http://blog.efnx.com


      [Non-text portions of this message have been removed]
    • Andre Nathan
      Hello ... You could turn trace into a printf-like function using ksprintf: open Printf let trace fmt = let f msg = let file = foo.log in let oc = open_out
      Message 2 of 4 , Aug 30, 2009
      • 0 Attachment
        Hello

        On Sun, 2009-08-30 at 13:59 -0700, Schell wrote:
        > I'd like to just have trace wrap fprintf with my file info and relay
        > all the
        > args to fprintf. Is this possible?

        You could turn trace into a printf-like function using ksprintf:

        open Printf

        let trace fmt =
        let f msg =
        let file = "foo.log" in
        let oc = open_out file in
        fprintf oc "%s\n" msg;
        close_out oc in
        ksprintf f fmt

        let () =
        trace "%s, %d, %f" "foo" 42 3.14


        HTH,
        Andre
      • Schell
        Wow, thanks, that totally works but I don t understand why. Magic. ... -- Schell Scivally efsubenovex@gmail.com http://blog.efnx.com [Non-text portions of this
        Message 3 of 4 , Aug 30, 2009
        • 0 Attachment
          Wow, thanks, that totally works but I don't understand why. Magic.

          On Sun, Aug 30, 2009 at 3:08 PM, Andre Nathan <andre@...> wrote:

          >
          >
          > Hello
          >
          >
          > On Sun, 2009-08-30 at 13:59 -0700, Schell wrote:
          > > I'd like to just have trace wrap fprintf with my file info and relay
          > > all the
          > > args to fprintf. Is this possible?
          >
          > You could turn trace into a printf-like function using ksprintf:
          >
          > open Printf
          >
          > let trace fmt =
          > let f msg =
          > let file = "foo.log" in
          > let oc = open_out file in
          > fprintf oc "%s\n" msg;
          > close_out oc in
          > ksprintf f fmt
          >
          > let () =
          > trace "%s, %d, %f" "foo" 42 3.14
          >
          > HTH,
          > Andre
          >
          >
          >



          --
          Schell Scivally
          efsubenovex@...
          http://blog.efnx.com


          [Non-text portions of this message have been removed]
        • Richard Jones
          ... I wrote something about how to make this sort of function: http://camltastic.blogspot.com/2008/09/writing-printf-like-functions.html In general it s not
          Message 4 of 4 , Aug 31, 2009
          • 0 Attachment
            On Sun, Aug 30, 2009 at 01:59:50PM -0700, Schell wrote:
            > Hi guys, I was just wondering if there is an equivalent language feature to
            > Actionscript 3's "... args" in OCaml. I'm trying to declare a function that
            > takes an unknown number of arguments and runs through them, and better yet,
            > gives them all to fprintf. Here's an example:
            >
            > let trace message =
            > let file = "/Library/Application Support/Commandline/trace.log" in
            > let oc = open_out file in
            > fprintf oc "%s\n" message;
            > close_out oc;
            > ;;
            >
            > I'd like to just have trace wrap fprintf with my file info and relay all the
            > args to fprintf. Is this possible?

            I wrote something about how to make this sort of function:

            http://camltastic.blogspot.com/2008/09/writing-printf-like-functions.html

            In general it's not possible to have a function which takes a variable
            number of arguments. It only works in the printf-case because of
            special behaviour in the compiler. You'd need to use a camlp4 macro
            to do it for any other function.

            Rich.

            --
            Richard Jones
            Red Hat
          Your message has been successfully submitted and would be delivered to recipients shortly.