alkozin
(Alexander Kozin)
1
Good day
It's very convenient to have pointers to functions that can be stored for delayed (@escaping) execution:
struct Foo {
func bar(argument: Int) {
}
func +(lhs: Foo, rhs: Foo) -> Foo {
}
}
let foo = Foo().bar
foo(1)
But for operators is not possible to do same:
let foo = Foo().+
ole
(Ole Begemann)
2
When referring to operators as function values, you sometimes have to enclose the operator in parentheses, like so: let foo = (+).
In addition, there are a few more errors in your code:
- Operator methods (when the operator function is part of a type) have to be
static, i.e. they don't get an implicit self parameter.
- I don't think you can refer to a specific operator overload with
Foo.(+) to distinguish it from all the other overloads of +.
This works:
struct Foo {
static func +(lhs: Foo, rhs: Foo) -> Foo {
// TODO: Implementation missing
fatalError()
}
}
// Add explicit type annotation to pick your overload
let add: (Foo, Foo) -> Foo = (+)
10 Likes