CTMacUser
(Daryle Walker)
1
We've recently added the capability for objects to be called as functions by adding a method to a nominal type with a base name of "callAsFunction" and any desired parameter tuple. Can such objects be used for code that takes in a closure? I haven't tried it; it's just a thought experiment right now. One part of me thinks they should not match because a closure is a function type and a nominal type with callAsFunction still isn't a function type. Another part of me thinks they should match because the feature would be 99% useless otherwise.
Which way is it? And if it's allowed, how does that get past the type checker?
Jumhyn
(Frederick Kellison-Linn)
2
No, such an implicit conversion is not currently supported.
No, you cannot do that yet.
struct S {
func callAsFunction() {}
}
func method(_: () -> Void) {}
let s = S()
method(s) // error
Lantua
4
Well, you could use x.callAsFunction.
6 Likes
Kentzo
(Ilya Kulakov)
5
Would this extend the lifetime of x?
Lantua
6
It would strongly capture x (as it should), so yes, x would at least outlive the function.
1 Like