Treat format strings as functions with parameters that return String

Consider sequence of tuples:

let tuples: [(Int, Int)] = [(0, 1)]

I would like to get a list of strings.

let strings: [String] = tuples.map({"\($0.0) -> \($0.1)"})

Good, right?
But what if we could treat strings with format as functions?

let string = "%d -> %d" // both String and (Int, Int) -> String
let function: (Int, Int) -> String = "%d -> %d"

After that I could just pass format to map function.

let strings: [String] = tuples.map("%d -> %d")

In general, this could be possible after functions as structs feature with usage of parsing and ExpressibleByStringLiteral

I think this would be very confusing to anyone reading the code (how does a String value get implicitly converted into a function?). I think what you want to do can already be done today, like:

let function: (Int, Int) -> String = { "\($0) -> \($1)" }
func transformer(a: Int, b: Int) -> String { "\(a) -> \(b)" }
let strings: [String] = tuples.map(transformer)
2 Likes

I think this is a neat idea, but it has the same problem as the $0 $1 shorthands: you have to use the in keyword when dealing with nested closures sometimes. e.g.