# Differences between type-inheritance clause & protocol composition syntax?

**URL:** <https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940>\
**Category:** Using Swift\
**Tags:** compiler, inheritance, protocols, declarations, conformance\
**Created:** [May 6, 2021, 3:35pm UTC](https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940 "2021-05-06T15:35:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [May 6, 2021, 3:35pm UTC](https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940/1 "2021-05-06T15:35:31Z")

</div>

Four years ago, Swift 4 introduced [protocol compositions](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#ID282) to Swift via SE-0156, with a review in early 2017, which finally added a long-awaited Obj. C feature via the new single-ampersand syntax for type declarations:

```swift
protocol A{}
protocol B{}
class C: A & B {}

```

However, for reasons that weren't immediately apparent from scanning the old review thread, we also kept what our [current documentation describes as a "type inheritance list"](https://docs.swift.org/swift-book/ReferenceManual/Types.html#ID456), which uses a comma separator as opposed to an ampersand:

```swift
class D: A, B {} 
class E {}
class F: E, A, B {} 

```

You can even combine them:

```swift
class E {}
class F: E, A, B {}
class G: E & A, B {} // ew
class H: E, A & B {} // yuck
class I: A & B & E {} // if it's all ampersands, the class doesn't have to be first
class J: A & B, E {} // won't compile bc there's a comma and the class isn't first

```

From what I can tell, these are the differences between the comma and ampersand syntax:

- if a comma is present, and a class is in the list of types, then the compiler requires that the the class appear first in the list
- the comma-separated variety can only appear in a type inheritance clause, but not in the type of a variable, typealias, tuple param, or function param

This begs the question of, _why would you ever choose comma over ampersand for a type inheritance clause?_ Is it a purely stylistic thing or is there some functional difference that I'm missing?

Thank you for any insights!

---

<div class="post-metadata">

**Author:** ![xAlien95](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xalien95/32/12558_2.png) [@xAlien95](https://forums.swift.org/u/xAlien95)\
**Post date:** [May 6, 2021, 3:49pm UTC](https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940/2 "2021-05-06T15:49:11Z")

</div>

> [@Is this a good idea? protocol IdentifiableHashable: Hashable, Identifiable { }](https://forums.swift.org/t/is-this-a-good-idea-protocol-identifiablehashable-hashable-identifiable/47496/11):
>
> While I don't really have any useful opinions on the main point you raised in your post, I can answer this question. slight_smile The answer is yes. A single inheritance clause with a protocol composition is equivalent to multiple inheritance clause entries where each one is a single protocol. There's also an interesting useless bit of trivia here. Could we hypothetically deprecate the , syntax for writing out multiple inheritance clause entries and always have a single one which might be a…

---

<div class="post-metadata">

**Author:** ![Moximillian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moximillian/32/3055_2.png) [@Moximillian](https://forums.swift.org/u/Moximillian)\
**Post date:** [May 6, 2021, 10:06pm UTC](https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940/3 "2021-05-06T22:06:33Z")

</div>

Whlie it’s not a technical difference, if you use type aliases, then you might need to use both, sort of.

```
protocol P { … }
protocol A { …. }
protocol B { …. }
typealias MyCombo = P & A

struct S1: P { … }
struct S2: MyCombo, B { … }
struct S3: MyCombo { … }

```

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [November 1, 2023, 1:59am UTC](https://forums.swift.org/t/differences-between-type-inheritance-clause-protocol-composition-syntax/47940/5 "2023-11-01T01:59:01Z")

</div>

It doesn’t work for enum raw types, cause those are special.

I’d argue it’s _poor style_ for classes to include their superclass via composition unless there’s a really good reason, but I believe it will work. EDIT: through a typealias, I mean. It doesn’t seem much different if written explicitly in the inheritance list.

Between lists of protocols I don’t think it matters.
