SE-0244: Opaque Result Types (reopened)

From my comment in the previous review thread:

If you can't name it, it's difficult to store as a variable in a struct or elsewhere, and it's difficult to write wrapper functions without each of them creating a new distinct opaque type.

In other words, when using functions returning opaque types, you now have an inability to write convenience wrapper functions that return the same type:

// module A, maintained by X
func makeShape(params: [Any: Any]) -> some Shape { ... }
// module B, maintained by Y
func makeCircle() -> some Shape {
  return makeObject(params: ["type": "circle"])
}
func makeSquare() -> some Shape {
  return makeObject(params: ["type": "circle"])
}
makeCircle() == makeSquare() // error: two distinct types
[makeCircle(), makeSquare()] // error: two distinct types

All maintainer Y can do is beg maintainer X to not use an anonymous type and use a named type (opaque or not). Either that or Y has to avoid writing helper functions of this sort.

There is no syntax to express the name and that makes the type more difficult to deal with in ways that didn't exist before in the language. So it is definitely different than a function which returns a custom type today.

2 Likes