Closures cannot use the `some` keyword?

Is this a known limitation? I'm using Swift 5.1 and when I'm trying to do this:

let closure = { (size: CGSize) -> some View in
...
}

I'm getting a couple of errors, the leading one being: Consecutive statements on a line must be separated by ';'.
However, if using a func, same declaration works perfectly. Is this a known issue or limitation?

func funcName(size: CGSize) -> some View {
...
}

Have you tried brackets around the two words?

Yeah, first thing I tried :) That doesn't work either, unfortunately.

Ah well, it was worth a try :nerd_face: I don't have Swift 5.1 installed yet.

Adding a type annotation to the closure variable gives a more informative error message:

protocol P { }
extension Int : P { }

let closure: () -> some P = { return 13 }
// 'some' types are only implemented for the declared type
// of properties and subscripts and the return type of functions

It doesn't really make sense right? Opaque return types are a compile-time constrict, and closures could be used at arbitrary times during runtime.

Closures cannot currently have opaque return types, though they could in the future.

Opaque types become the underlying type at runtime. There shouldn't be any implementation limitation preventing closures from having opaque return types.

6 Likes

Thank you guys, all is clear now!

I have to say, it makes zero sense for closures not to be able to use "some" as a return type.

2 Likes