How to produce closure with opaque result type?

Hey,

Since SE-0328 we have the ability to declare functions that return structural opaque result types.
In particular, the proposal calls out, that it should now be possible to write a function like this:

func f() -> () -> some P { /* ... */ }

But, however hard I try, it doesn't seem possible to actually write such a function. E.g. I want to write a function with the following signature:

func foo() -> () -> some BinaryInteger { /* ...*/ }

There are different ways to go on about this. The most straightforward way gives an error:

func foo() -> () -> some BinaryInteger {
    { 5 }
}
// Error: function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

Explicitly annotating the closure type doesn't work as well. Neither of these solutions seem to work:

func foo() -> () -> some BinaryInteger {
    { () -> some BinaryInteger in 
        5
    }
}
// Error: function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
// Error: 'some' types are only permitted in properties, subscripts, and functions

func foo() -> () -> some BinaryInteger {
    { 5 } as () -> some BinaryInteger
}
// Error: 'some' types are only permitted in properties, subscripts, and functions

Another way would be like this:

func foo() -> () -> some BinaryInteger {
    { 5 as Int }
}
// Error: cannot convert value of type 'Int' to closure result type 'some BinaryInteger'

Even creating an auxiliary function doesn't work:

func fooImpl() -> some BinaryInteger {
    5
}

func foo() -> () -> some BinaryInteger {
    { fooImpl() }
}
// Error: cannot convert value of type 'some BinaryInteger' (result of 'fooImpl()') to closure result type 'some BinaryInteger' (result of 'foo()')

It doesn't seem to be possible in any way to write the body of this function. Am I doing something wrong? Is this just a bug in the compiler?

Thanks in advance for your help!

4 Likes