Template strings in F#?

f#

Solution

The idiomatic way of doing that in F# is with `sprintf`:

let newString = sprintf "First Name: %s Last Name: %s" "John" "Doe"

Additionally, you have access to the .net `String.Format`:

let newString = String.Format("First Name: {0} Last Name: {1}", "John", "Doe")

The benefit of the first one is that it is type-safe (i.e. you can't pass a string to an integer formatter like "%d"). As noted by Benjol in the comments, it's not possible to pass a format string to `sprintf` because it is statically typed. See here for more information on that.

Problem

I'm new to F#, and want to know if there is anything in F# similar to template strings in Python. So I can simply do something like: ``` >>> d = dict(who='tim', what='car') >>> Template('$who likes $what').substitute(d) 'tim likes car' ```

Original source

Related problems