Skip to content

Instantly share code, notes, and snippets.

@soxjke
Created October 15, 2017 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soxjke/6c2dff8a8c255ff386f3c325d09300cf to your computer and use it in GitHub Desktop.
Save soxjke/6c2dff8a8c255ff386f3c325d09300cf to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
func work<R, T0>(_ f: @escaping (T0) -> R) -> (T0) -> R {
return { (arg0: T0) -> R in return f(arg0) }
}
func work<R, T0, T1>(_ f: @escaping (T0, T1) -> R) -> (T0, T1) -> R {
return { (arg0: T0, arg1: T1) -> R in return f(arg0, arg1) }
}
func test1(_ arg0: Int) -> Int { return arg0 }
func test2(_ arg0: Int, arg1: Int) -> Int { return arg0 + arg1 }
let a = work(test1)
let b = work(test2) // error: SwiftOverloads.playground:11:9: error: ambiguous use of 'work'
@soxjke
Copy link
Author

soxjke commented Oct 15, 2017

Works with labels, but ...

import Foundation

func work<R, T0>(f1: @escaping (T0) -> R) -> (T0) -> R {
    return { (arg0: T0) -> R in return f1(arg0) }
}

func work<R, T0, T1>(f2: @escaping (T0, T1) -> R) -> (T0, T1) -> R {
    return { (arg0: T0, arg1: T1) -> R in return f2(arg0, arg1) }
}

func test1(_ arg0: Int) -> Int { return arg0 }
func test2(_ arg0: Int, arg1: Int) -> Int { return arg0 + arg1 }

let a = work(f1: test1)
let b = work(f2: test2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment