# Opaque result types

**URL:** <https://forums.swift.org/t/opaque-result-types/15645>\
**Category:** Pitches\
**Created:** [August 29, 2018, 9:49pm UTC](https://forums.swift.org/t/opaque-result-types/15645 "2018-08-29T21:49:17Z")\
**Posts on this page:** 1\
**Showing post:** 177

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [September 7, 2018, 6:12pm UTC](https://forums.swift.org/t/opaque-result-types/15645/177 "2018-09-07T18:12:48Z")

</div>

> [@anandabits](#):
>
> FWIW, an opaque typealias that allows conformances _distinct from the underlying concrete type_ to be declared would be very much like a `newtype` (aside from _also_ having distinct type identity). I have been wondering if there would be any implementation overlap between opaque types and `newtype` and whether `newtype` might make sense as a future direction.

`newtype` is most closely related to [opaque type aliases](https://github.com/DougGregor/swift-evolution/blob/opaque-result-types/proposals/NNNN-opaque-result-types.md#opaque-type-aliases), because both introduce a new named type that is primarily described by its capabilities but has the same storage as some underlying type. One can think of `new type` as sugar for a struct containing a single stored property of the underlying type, so something like:

```swift
newtype Meters = Int

```

would desugar to something like:

```swift
struct Meters {
  var underlying: Int
}

```

Generally, one would expect some kind of opt-in forwarding of APIs from the underlying type to the `newtype`. For example:

```swift
newtype Meters: Numeric = Int

```

Now, `Meters` should conform to `Numeric`, using the implementations from the conformance of `Int` to `Numeric`. But this starts to fall apart when `Self` is used in the the protocols. For example:

```swift
protocol RandomValueSource {
  func getRandomValues(_ numValues: Int) -> [Self]
}

extension Int: RandomValueSource {
  func getRandomValues(_ numValues: Int) -> [Int] {
    return Array((0..<numValues).map(_ in Int.random(in: 0..<100)))
  }
}

```

How do we forward that conformance of `RandomValueSource` for `Meters`? It returns an `[Int]`, and now we need a `[Meters]`, even though it is a completely different type.

Because of this, I've mostly lost interest in `newtype`. Maybe Swift should get some kind of forwarding syntax to make it easy to write something like `Meters` as a wrapper type for `Int`, with opt-in synthesis for all of the members where it _does_ work. That would be a nice way to eliminate some boilerplate from the system without introducing a new concept.

Opaque result types don't have these issues because there is no new runtime type involved.

Doug

---

_[View the full topic](https://forums.swift.org/t/opaque-result-types/15645)._
