Optional opaque types?

I went to try using opaque types in my code using the Xcode 11 beta, and immediately ran into not being able to use optionals.

For example, I have this line (where Blob is a protocol):

func fileBlob(ref: String, path: String) -> Blob?

If I insert "some" before "Blob?", I get the error: "An 'opaque' type must specify only 'Any', 'AnyObject', protocols, and/or a base class".

I followed the discussion while this feature was in review, and never even thought about how it would work with optionals. I guess I just assumed some Blob? would be equally valid. Has anyone else run into this? Is there a workaround?

7 Likes

Similarly, It would be good to be able to return Result<some Blob, MyError>. Since some Blob? is equivalent to Optional<some Blob>, I guess what we're looking for is being able to use an opaque type as a generic type parameter.

1 Like

IIRC, opaque type in that context is not supported. Though it was in Future Direction.

I looked it up. Optional opaque return types are explicitly forbidden, and they are not discussed in Future Directions.

Opaque return types in protocols are also forbidden, but the compiler in Xcode 11 doesn't seem to mind them.

Restrictions on opaque result types ... could be lifted in the future

I found this in the swift-evolution.