The behavior of printf
functions in F # is somewhat special. They take a format string that contains the expected arguments. You can use Printf.kprintf
as shown by desco to define your own function that accepts a format string, but you cannot change the handling of format strings.
If you want to do something like C # params
(where the number of arguments is a variable, but does not depend on the format string), you can use the ParamArray
attribute directly for the member:
open System type Foo = static member Bar([<ParamArray>] arr:obj[]) = arr |> Seq.mapi (fun iv -> printfn "[%d]: %A" iv)
Then you can call Foo.Bar
with any number of arguments without a format string:
Foo.Bar("hello", 1, 3.14)
This is less elegant for formatting strings, but may be useful in other situations. Unfortunately, it will only work with members (and not with functions defined with let
)
source share