# Protocol conformance and \`Optional\`s

**URL:** <https://forums.swift.org/t/protocol-conformance-and-optional-s/30976>\
**Category:** Using Swift\
**Tags:** optional, protocols\
**Created:** [November 20, 2019, 10:18pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976 "2019-11-20T22:18:55Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![technicated](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technicated/32/5420_2.png) [@technicated](https://forums.swift.org/u/technicated)\
**Post date:** [November 21, 2019, 8:41pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/21 "2019-11-21T20:41:31Z")

</div>

@Joe_Groff do we have a better name for this kind of relationship? I always thought of this (and Array, etc) as subtypes...

---

<div class="post-metadata">

**Author:** ![technicated](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technicated/32/5420_2.png) [@technicated](https://forums.swift.org/u/technicated)\
**Post date:** [November 21, 2019, 11:57pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/22 "2019-11-21T23:57:46Z")

</div>

_ **Note to me and to everyone else** _

I want to make clear that using the "trick" does **not** work, in the following sense:

```swift
struct V {
  let fixedValue: Int = 10
}

protocol P {
  var v: V? { get }
}

extension P {
  var v: V? { nil }
}

struct S1: P {
  // note, this *is not* optional
  let v: V = V()
}

struct S2: P {
  // note, this *is* optional
  let v: V? = V()
}

```

The conformance of `S1` to `P` _is only valid through the default implementation_. The non-optional overload is **never** seen from the protocol, so if a function on an extension of `P` makes use of `v` in the context of `S1`, **`nil`** and not `V()` will be used!

On the other hand `S2` conforms to the protocol deciding not to use the default implementation and declaring a property `v` of type `V?` itself. Calling a function of `P` in the context of `S2` will indeed use `.some(V())`.

```swift
extension P {
  func printV() {
      print(v)
  }
}

S1().fn()
// nil
S2().fn()
// Optional(V(fixedValue: 10))

```

---

<div class="post-metadata">

**Author:** ![CrystDragon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crystdragon/32/10461_2.png) [@CrystDragon](https://forums.swift.org/u/CrystDragon)\
**Post date:** [November 22, 2019, 9:13am UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/23 "2019-11-22T09:13:14Z")

</div>

I asked a similar question here: [Why covariance does not apply when working with protocols in some situations](https://forums.swift.org/t/why-covariance-does-not-apply-when-working-with-protocols-in-some-situations/27689)

I believe the answer is that in the current compiler implementation, when dealing with protocols, Liskov substitution principle is not fully supported in some circumstances. Some deeper reasons are also explained in that thread by other guys.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [November 22, 2019, 5:40pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/24 "2019-11-22T17:40:18Z")

</div>

I would still call these subtype relationships. To me, it seems like a bug that protocol conformances don't allow for variance between the implementation's and requirement's types; hopefully that'll be something we fix one day.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 22, 2019, 6:09pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/25 "2019-11-22T18:09:56Z")

</div>

> [@Joe\_Groff](#):
>
> hopefully that'll be something we fix one day.

That would be great! In your opinion would this also apply to the OP situation? Using a non-optional to fit a Protocol's requirement that's Optional.

> [@Joe\_Groff](#):
>
> I would still call these subtype relationships.

With typical generic Types (e.g. `Array<T>`, `Set<T>`) the abstraction makes sense to me. I'm unsure how to reconcile it with the complication of `Optional<T>` being an `enum`. In many cases it behaves similar to a typical parent-child relationship, but the semantics seem meaningfully different.

Maybe my thinking is overly-influenced by OOP and class-subclass relationships. Does "subtype" have a more generalized definition here? 🤔

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 22, 2019, 6:22pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/26 "2019-11-22T18:22:40Z")

</div>

IIRC (aka, don't quote me on this 😝) subtyping relationship only requires substitutability, which applies nicely to `T?` and `T`. Only that class inheritance usually support subtyping relationship out of the box.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 22, 2019, 6:28pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/27 "2019-11-22T18:28:29Z")

</div>

Don‘t think about that particular subtype relationship as strict as it could be. Otherwise `Never` could never become the bottom type. These are just language specific details. Similarly ImplicitUnwrappedOptional is also an Optional.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 22, 2019, 6:31pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/28 "2019-11-22T18:31:16Z")

</div>

> [@Lantua](#):
>
> subtyping relationship only requires substitutability

Yes you can substitute `Optional<T>.some` with T, but you _can't_ substitute `Optional<T>.none` with T. It also doesn't adhere to `ExpressableByNilLiteral` (nor should it), so the relationship is more complicated.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 22, 2019, 6:35pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/29 "2019-11-22T18:35:17Z")

</div>

Isn‘t the substitution relation the other way around? You can kinda upcast `T` to `T!` and finally to `T?`.

There is also an 'enum subtyping' where a subtype enum of a given parent enum would not add new cases but rather remove cases. That‘s exactly what we have here, we removed the `none` case and are left with `some(T)` which in fact is just `T`.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 22, 2019, 6:38pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/30 "2019-11-22T18:38:22Z")

</div>

Btw. here is a great (non-official) document of different related ideas from @anandabits: [ValueSubtypeManifesto.md · GitHub](https://gist.github.com/anandabits/5b7f8e3836387e893e3a1197a4bf144d)

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 22, 2019, 6:41pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/32 "2019-11-22T18:41:00Z")

</div>

> [@DevAndArtist](#):
>
> Otherwise `Never` could never become the bottom type.

Agh...`Never` might be [the worst thing to search for](https://duckduckgo.com/?q=Swift+Never&t=osx&ia=videos) 😂

> [@DevAndArtist](#):
>
> Isn‘t the substitution relation the other way around?

At least with typical OOP subtypes, If `T?` is a subtype of `T` then you should be able to use `T?` wherever `T` is used. The current relationship is the opposite. Unless you're saying `enum subtyping` (something I was unaware of) is basically the oposite?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 22, 2019, 6:44pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/33 "2019-11-22T18:44:40Z")

</div>

Yeah in terms of value subtyping (not regular objects) for enum subtypes you remove cases which guarantees that a super enum will have all cases of any of its subtype. In our case the upcast from `T` to `T?` would just magically wrap a non-optional value into the `Optional.some(t)`.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 22, 2019, 6:46pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/34 "2019-11-22T18:46:00Z")

</div>

> [@DevAndArtist](#):
>
> Btw. here is a great (non-official) document of different related ideas from @anandabits: [ValueSubtypeManifesto.md · GitHub](https://gist.github.com/anandabits/5b7f8e3836387e893e3a1197a4bf144d)

> It is important to observe that the semantics of value subtype relationships are much different than object oriented inheritance relationships. They have nothing to do with dynamic dispatch or implementation sharing. Another important difference is that providing a subclass instance where a superclass instance is required never requires a change in representation of the object reference itself.

Ah. I think this is what I was looking for! So, a Value subtype is a subset of it's parent, while an Object subtype is a superset of it's parent. Seems like we're in need of a new term 🤔

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 22, 2019, 6:51pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/35 "2019-11-22T18:51:29Z")

</div>

> [@GetSwifty](#):
>
> Agh... `Never` might be [the worst thing to search for](https://duckduckgo.com/?q=Swift+Never&t=osx&ia=videos) 😂

About that, you should rather search for 'bottom type'.

> <https://github.com/swiftlang/swift-evolution/blob/74d62ec62b742848ee03005c478eccdc5f7d504d/proposals/0102-noreturn-bottom-type.md>

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [November 27, 2019, 10:44pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/36 "2019-11-27T22:44:17Z")

</div>

> [@DevAndArtist](#):
>
> The answer isn‘t that simple actually, it‘s not just a strict misalignment of protocol requirements here, because theoretically `T` is a subtype of `T?`

> [@GetSwifty](#):
>
> At least with typical OOP subtypes, If `T?` is a subtype of `T` then you should be able to use `T?` wherever `T` is used.

But it was never said that `T?` was a subtype of `T`. It was stated the other way around: `T` is a subtype of `T?` And you can in fact use a `T` anywhere a `T?` is needed.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [December 2, 2019, 3:50pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/37 "2019-12-02T15:50:19Z")

</div>

> [@Nobody1707](#):
>
> `T` is a subtype of `T?` And you can in fact use a `T` anywhere a `T?` is needed.

It really depends on your definition. e.g. if it was a typical class-subclass relationship, when `T` is a subtype of `T?` you would be able to do `T.instance?.doAThing()` since all the functionality of `T?` should be available to `T`.

It's also not 100% replaceable anyway:

```swift
func doAThing<T: ExpressibleByNilLiteral>(theThing: T) {
    print("Did A Thing")
}

let otherThing: String? = "hi"
let realThing: String = "hi"

doAThing(theThing: otherThing)
doAThing(theThing: realThing) // error: argument type 'String' does not conform to expected type 'ExpressibleByNilLiteral'
// this would work if String was a subclass of String?

```

---

<div class="post-metadata">

**Author:** ![technicated](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technicated/32/5420_2.png) [@technicated](https://forums.swift.org/u/technicated)\
**Post date:** [December 2, 2019, 4:16pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/38 "2019-12-02T16:16:01Z")

</div>

I might be inventing things here but... maybe **subclass** and **subtype** are different kinds of _relationships_, where **subclass** is a "stronger" kind of relationship than **subtype**.

A _subtype_ does not have all the functionalities of its "supertype", but can be automatically converted / wrapped into it to acquire these functionalities. A subclass instead always _directly_ has all the functionalities of its "supertype" (i.e. its _superclass_).

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [December 2, 2019, 5:07pm UTC](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976/40 "2019-12-02T17:07:21Z")

</div>

> [@GetSwifty](#):
>
> [ValueSubtypeManifesto.md · GitHub](https://gist.github.com/anandabits/5b7f8e3836387e893e3a1197a4bf144d)

The conclusion I came to from that is available earlier in the thread. I'm unsure how official or industry-defined it is to distinguish class-subtypes and value-subtypes, but the distinction makes sense in my brain 🙂

[Previous page](https://forums.swift.org/t/protocol-conformance-and-optional-s/30976.md?page=1)
