Can function objects be passed as arguments for a closure-typed parameter?

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?

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

Well, you could use x.callAsFunction.

6 Likes

Would this extend the lifetime of x?

It would strongly capture x (as it should), so yes, x would at least outlive the function.

1 Like