Reputation: 1044
I have function curry
that takes a function (A, B) -> C
as an argument, and returns A -> B -> C
. It works fine on function g(a: Int, b: Int) -> Int
. Also, I have function f
, that is polymorphic, so its type is ambigous. Here is the code:
func g(a: Int, b: Int) -> Int { return a + b }
func f(a: Int, b: Int) -> Int { return a + b }
func f(a: Float, b: Float) -> Float { return a + b }
func curry<A, B, C>(f: (A, B) -> C) -> A -> B -> C {
return { a in { b in f(a, b) } }
}
let curriedG = curry(g) //curriedG type Int -> Int -> Int
let curriedF = curry(f) //Error, Ambigous use of 'f(_:b:)'
It is obvious, that I can't do curry(f)
, but, is there any way, so I could specify, that I want to use specific f()
implementation?
Why I need this.
Because of initialisers. I want to curry specific initialiser, lets say struct A
has two initialisers: init(t: T, y: Y)
and init(u: U, h: H)
, I want to curry one of them.
Thanks!
Upvotes: 1
Views: 117
Reputation: 539945
Since f
is overloaded, you have to specify which f
you want:
let curriedF = curry(f as (Float, Float) -> Float)
or, alternatively, what the result should be:
let curriedF = curry(f) as Float -> Float -> Float
Upvotes: 1