Int { a + b } curry(sum) It shouldn'" /> Int { a + b } curry(sum) It shouldn'" /> Int { a + b } curry(sum) It shouldn'"/>

I've managed to pass two-arguments function, where one-argument function is expected

32 views Asked by At
func curry<A, B>(_ f: @escaping (A) -> B) -> (A) -> B {
    print("is called")
    return f
}

func sum(a: Int, b: Int) -> Int {
    a + b
}

curry(sum)

It shouldn't even compile. But it compiles and the curry function gets called!
f is inferred as ((Int, Int)) -> Int

While the point of view of the compiler is pretty obvious here, it still feels like a bug...
And how am I supposed to prevent this situation to happen? I mean successfully passing parameters with, basically, not-matching types!

P.S. I was attempting to implement curry function in general form (with variadic type parameters) through recursion. The curry function provided above was intended to be the final step of the process. Hence the naming

0

There are 0 answers