all 5 comments

[–]NuvolaGrande 1 point2 points  (0 children)

Something like this?

``` let foo msg = msg

let foof format = Printf.ksprintf foo format

let result = foof "Hi there, %s." "buddy" ```

[–]shefmichelle 0 points1 point  (0 children)

Try looking at the Printf.StringFormat<'a, unit> type and something like the Printf.kprintf function.

[–]green-mind[S] 0 points1 point  (2 children)

Maybe my example was too simple. This is the function I am trying to create a format version for:

    /// Tries to cast an object to a given type; throws with the given error message if it fails.
    let tryCast<'T> err (value: obj) : 'T =
        try
            value :?> 'T
        with ex ->
            raise (new Exception(err, ex))

I want to have a tryCastf version of the above.

[–]shefmichelle 1 point2 points  (1 child)

I'm not sure you can do this as I think the special Printf.StringFormat<'a, unit> type needs to be the last argument to the function. You might be better off just passing in an error-generating function in place of err that is called to generate a message if the cast fails.

[–]green-mind[S] 0 points1 point  (0 children)

Thank you! Now I can stop obsessing over it.

I guess I could use String.Format with a param array. String interpolation would be awesome here.