Swift - Assigning Overloaded Function to Variable

I don't know how to do exactly what you want, but maybe this helps:

var myFunc1: (String)-> String = { s in f(sourceString: s) }
var myFunc2: (String)-> String = { s in f(s) }

You can now call:

let s1 = myFunc1("one")  // returns "version 2: one"
let s2 = myFunc2("two")  // returns "version 1: two"

Interesting one this. I don’t think it’s possible without doing something along the lines of @marcos’s suggestion. The problem you is you can “cast away” the names in tuples:

let named_pair = (s: "hello", i: 1)
named_pair.s  // hello

let anon_pair = named_pair as (String,Int)
// or anon_pair: (String,Int) = named_pair, if you prefer
anon_pair.s  // no such member 's'

Now suppose you define two functions, identical except one has named arguments:

func f(s: String, i: Int) { println("_: \(s)") }
func f(#s: String, #i: Int) { println("s: \(s)") }

You can then call it via tuples with named vs unnamed arguments:

f(named_pair)  // prints s: hello
f(anon_pair)   // prints _: hello

// but if you try to call a named argument function with unnamed tuples:
func g(# s: String, # i: Int) { println("s: \(s)") }
g(anon_pair)  // compiler error

let h = g
h(anon_pair)   // compiler error
h(named_pair)  // works

But because you can cast away these names you can do this:

// compiles and runs just fine...
(g as (String,Int)->())(anon_pair)
let k: (String,Int)->() = g
// as does this
k(anon_pair)

And this ability to do this means it’s not possible to use a type to disambiguate an function overloaded only by argument names, as far as I can tell.


Referencing func f (s: String) -> String { return "version 1: " + s }:

let myFunction = f(s:)

Referencing func f(sourceString s: String) -> String { return "version 2: " + s }:

let myFunction = f(sourceString:)

Referencing func anotherFunction(_ param: Any) {}:

let myFunction = anotherFunction(_:)

If you haven't overloaded the function, you don't need to explicity write out the parameter names when referencing the function.

Tags:

Swift