Hi! I've been reading through the (excellent) posts on Doug's Compiler Corner[1] from @Douglas_Gregor . I learn new things! I did not have much experience with "curried instance methods"[2]:
struct Point {
var x: Int
var y: Int
func flippedOverXAxis() -> Point { ... }
func flippedOverYAxis() -> Point { ... }
}
let memberFunction = Point.flippedOverXAxis
var p = Point(x: 1, y: 2)
p = memberFunction(p)()
I have a situation with a function that accepts a closure that operates on a Point instance:
func f(_: (Point) -> Point) { }
f({ point in Point.flippedOverXAxis(point)() })
This works… but I am wondering if there is some way to pass that curried instance method directly without the ceremony of wrapping it in a closure and passing a point instance:
f(Point.flippedOverXAxis)
This fails:
18 | f({ point in Point.flippedOverXAxis(point)() })
19 |
20 | f(Point.flippedOverXAxis)
| `- error: cannot convert value of type '@Sendable () -> Point' to expected argument type '(Point) -> Point'
| `- error: instance member 'flippedOverXAxis' cannot be used on type 'Point'; did you mean to use a value of this type instead?
21 |
Is there any way to pass that curried method directly to a closure that expects an "instance" method? I am unblocked (because I can always wrap the curried method in a closure)… but it would be interesting if there was a shortcut there that worked. Thanks!
Sure… but if we are changing the function signature then that's not exactly my original question. If we were stuck with the original function (because it came from a library or other source we don't control)… then we still need some kind of ceremony to forward that to the original function: