[swift-users] How to reference a generic function when a concrete version also exists?

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Tue May 2 18:39:24 CDT 2017


If I write a generic function like this:

func f<T>(_ x: T) { print("Generic: \(x)") }

I can pass it to another function like this:

func g(_ fn: (Int) -> Void) { fn(0) }
g(f)    // Prints “Generic: 0”

However if I *also* write a non-generic function like this:

func f(_ x: Int) { print("Int: \(x)") }

Then when I make the same call as before:

g(f)    // Prints “Int: 0”

It passes in the new, non-generic version.

Is there something I can do, with both versions of f defined, to pass the
generic f into g?

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170502/d8118080/attachment.html>


More information about the swift-users mailing list