# \[Pitch 2\] Light-weight same-type requirement syntax

**URL:** <https://forums.swift.org/t/pitch-2-light-weight-same-type-requirement-syntax/55081>\
**Category:** Pitches\
**Created:** [February 3, 2022, 5:20pm UTC](https://forums.swift.org/t/pitch-2-light-weight-same-type-requirement-syntax/55081 "2022-02-03T17:20:44Z")\
**Posts on this page:** 1\
**Showing post:** 105

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 19, 2022, 11:07am UTC](https://forums.swift.org/t/pitch-2-light-weight-same-type-requirement-syntax/55081/105 "2022-02-19T11:07:12Z")

</div>

I agree that labels for generic parameters would be a nice addition to the language.

One generic type is generally fine (`Array<Int>`), two generic types can be okay if you are familiar with the type already (`Dictionary<String, Int>`), but three or more generic types start becoming an incomprehensible soup (`Foo<String, Int, String>`).

They remind me of C functions: `foo(42, -7, true)`. Swift improves on those by adding labels, and it makes just as much sense for generic types as it does for functions IMO.

> [@Jumhyn](#):
>
> OTOH, I think the 'natural' syntax for generic types also makes sense for protocols. `Collection<Element: Int>` reads just fine to me, and is marginally terser than `Collection<.Element == Int>` .

We can bikeshed the syntax after we agree on the direction, IMO. `"<Element: Int>"` has things going for it; [I've also suggested](https://forums.swift.org/t/pitch-light-weight-same-type-constraint-syntax/52889/67) that the compiler could just DWIM when users mistakenly constrain to a concrete type. There are other possibilities and considerations, though.

> [@Jumhyn](#):
>
> It's important for the API author to be in control here—as @Karl notes, there are plenty of protocols (e.g. `Identifiable` ) where simply allowing anonymous specification of any associated types at all would hurt more than it helped.

Swift does favour putting API authors in control. If we already had the general constraint syntax, you could imagine allowing API authors to define aliases for common constraints, as they see fit.

This could involve minimal code changes, unlike the highly invasive changes proposed here.

```swift
#if swift(>= X)
  typealias Collection<T> = Collection<.Element == T>
#endif

// Can totally live alongside the regular Collection protocol.
protocol Collection: Sequence { ... }

```

But again, I think this is a very separable feature and warrants its own discussion.

> [@Jumhyn](#):
>
> I don't see a good reason why either the "named associated type constraints" or "anonymous associated type constraints" design directions have to _preclude_ the other. It seems like it should be perfectly possible to introduce either one after the other, so it's not as though adopting the `Collection<Int>` syntax would prevent us from later allowing `Collection<Element: Int, Index: Int>` .

I've already outlined the issues as I see them in previous posts in this thread:

1. This is an opt-in, highly invasive feature for library developers.
2. You don't get _any_ constrained opaque types _unless_ you opt-in and make those very invasive changes.
3. This is a more controversial shorthand (IMO), and should be evaluated in isolation.

This proposal includes an entirely new way of _declaring_ protocols, not just using them, while at the same time being very restrictive, not scalable, and going against an important Swift philosophy that we should favour clarity over brevity at the point of use. It's the wrong place to start.

I think the most logical way to approach this is to implement the general feature and syntax (Slava says it's already somewhat implemented behind flags - great!), and discuss this limited shorthand later. Bundling the shorthand together with the slam-dunk, very, very desirable feature of constraints on opaque types, is just poor practice.

---

_[View the full topic](https://forums.swift.org/t/pitch-2-light-weight-same-type-requirement-syntax/55081)._
