Is it possible to partially apply a function such as bprintf
and prevent it from being restricted based on its initial use?
I'd like to do the following:
let builder = new System.Text.StringBuilder()
let append = Printf.bprintf builder
append "%i" 10
append "%s" string_value
The aspect of F# that's causing this is called value restriction. You can see that if you enter just the two
let
declarations to F# Interactive (so that the compiler doesn't infer the type from the first use):There is an excellent article by Dmitry Lomov from the F# team which explains it in detail. As the article suggests, one solution is to add explicit type parameter declaration:
This will work just fine.
You're encountering the F# value restriction.
Here's a good explanation of some workarounds: Understanding F# Value Restriction Errors
Here's a fairly in-depth article explaining the reasons behind it: http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx
you can add explicit format argument