Like below; strings are implicitly convertible to PrintfFormat, wherein lies the magic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 

let getFormat x : Microsoft.FSharp.Core.PrintfFormat<_,_,_,_,_> =
    () //...
    "{%s yadda %s}"  // How to turn this into a SF?

let print x =
    let f = getFormat x
    sprintf f "foo" "bar"

let s = print 42

printfn "%s" s
By on 7/8/2010 9:34 AM ()

Awesome!
Now my problem is: inside getFormat I have a pattern matching clause, and one of the cases generates the format dynamically (using sprintf itself), so the compiler can't see the format (although it IS of the same type of all the others). Can this be done?

1
2
3
4
5
let getFormat x : PrintfFormat<_,_,_,_,_> =
    match x with
         | "x" -> "%s%s"
         |  "y" -> "%s.%s"
         | any -> sprintf "%s%s%s" "%s" any "%s"
By on 7/9/2010 2:59 AM ()
1
2
3
4
5
6
7
8
9
let getFormat x : PrintfFormat<_,_,_,_,_> =
    match x with
         | "x" -> "%s%s"
         | "y" -> "%s.%s"
         | any -> new PrintfFormat<_,_,_,_,_>(sprintf "%s%s%s" "%s" any "%s")
 
printfn (getFormat "x") "foo" "bar"
printfn (getFormat "y") "foo" "bar"
printfn (getFormat "z") "foo" "bar"
By on 7/9/2010 3:42 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper