# Generic type as generic constraint

**URL:** <https://forums.swift.org/t/generic-type-as-generic-constraint/54765>\
**Category:** Discussion\
**Created:** [January 21, 2022, 2:43am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765 "2022-01-21T02:43:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 2:43am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/1 "2022-01-21T02:43:56Z")

</div>

Okay, I know the title is a little confusing, bear with me here.

You can write a generic constraint with either an equality or a subtype relation:

```swift
extension Collection where Element == Int {}

extension Collection where Element: BinaryInteger {}

```

But you cannot, today, write a constraint that specifies a particular generic type, leaving its generic parameters unspecified.

For example, suppose you want to extend “collections of optionals”. You cannot write:

```swift
extension Collection where Element: Optional {} 
// error: reference to generic type 'Optional' requires arguments in <...>

```

And the same error occurs if we replace the “`:`” with “`==`”.

• • •

First of all, why would one even want to do this?

Here’s a simple example:

`Dictionary` has a method called `compactMapValues`, which makes a new dictionary by transforming each value, and dropping the entries that got mapped to nil. That works for all dictionaries, and it requires passing in a transformation closure.

But suppose we have a dictionary of optionals (perhaps loaded from disk or fetched from a server) and we find ourselves calling `dict.compactMapValues{ $0 }` to remove the nils and unwrap the values.

We don’t actually transform the values, so we might decide to make a little convenience method that lets us simply write `dict.compacted()` instead:

```swift
func compacted() -> [Key: Value.Wrapped] {
  return self.compactMapValues{ $0 }
}

```

This is nice and clean, and puts things at the proper level of abstraction for us. The only problem is, where does that function go?

We want to put it in a constrained extension of `Dictionary` like so:

```swift
extension Dictionary where Value: Optional {
  func compacted() -> [Key: Value.Wrapped] {
    return self.compactMapValues{ $0 }
  }
}

```

But as mentioned above, the constraint `Value: Optional` results in a compile-time error.

• • •

There is a workaround, but it’s pure boilerplate which involves a new protocol that only one type conforms to, and pollutes the namespace of `Optional` with a superfluous property that just returns `self`:

```swift
protocol OptionalProtocol {
  associatedtype Wrapped
  var optionalValue: Wrapped? { get }
}

extension Optional: OptionalProtocol {
  var optionalValue: Self { self }
}

```

With that in place, we can write:

```swift
extension Dictionary where Value: OptionalProtocol {
  func compacted() -> [Key: Value.Wrapped] {
    return self.compactMapValues{ $0.optionalValue }
  }
}

```

And finally our method is available for use as `dict.compacted()`.

• • •

If we wanted to do something similar for another generic type—say an extension on “collections of dictionaries”—then we’d need another boilerplate protocol and another `self`-returning property.

The workaround works, but it doesn’t scale well, and it isn’t necessarily obvious. So I propose that we allow generic types to be used as constraints directly, without any ceremony or boilerplate. This should just work:

```swift
extension Dictionary where Value: Optional {}

```

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [January 21, 2022, 3:03am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/2 "2022-01-21T03:03:23Z")

</div>

I’m not sure where things currently stand with this feature, but I think [parameterized extensions](https://forums.swift.org/t/parameterized-extensions/25563) should address this use case.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 21, 2022, 3:04am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/3 "2022-01-21T03:04:23Z")

</div>

By itself, this:

> [@Nevin](#):
>
> ```swift
> extension Dictionary where Value: Optional {}
> 
> ```

doesn't give you any way to access the underlying wrapped type, because this:

> [@](#):
>
> ```swift
> func compacted() -> [Key: Value.Wrapped]
> 
> ```

isn't valid; `Wrapped` is a generic type parameter but not a member of `Optional`. So this change is more involved than it suggests, since it would require making all type arguments into members.

I think this would be better expressed as a parameterized extension, as [already described](https://github.com/apple/swift/blob/main/docs/GenericsManifesto.md#parameterized-extensions) in the generics manifesto, since those cover a superset of the functionality described here.

```swift
extension <T> Dictionary where Value: Optional<T>

```

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [January 21, 2022, 3:18am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/4 "2022-01-21T03:18:14Z")

</div>

> [@allevato](#):
>
> So this change is more involved than it suggests, since it would require making all type arguments into members.

Notably [featured](https://belkadan.com/blog/2021/10/Swift-Regret-Generic-Parameters-are-not-Members/) in @jrose’s Swift Regrets series. 🙂

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 3:20am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/5 "2022-01-21T03:20:58Z")

</div>

> [@allevato](#):
>
> By itself, this:
> 
> > [@Nevin](#):
> >
> > ```swift
> > extension Dictionary where Value: Optional {}
> > 
> > ```
> 
> doesn't give you any way to access the underlying wrapped type

I don’t claim to know the best way to _implement_ this, but the feature I am proposing does indeed allow accessing `Value.Wrapped` in that extension.

> [@allevato](#):
>
> I think this would be better expressed as a parameterized extension, as [already described](https://github.com/apple/swift/blob/main/docs/GenericsManifesto.md#parameterized-extensions) in the generics manifesto, since those cover a superset of the functionality described here.
> 
> ```swift
> extension <T> Dictionary where Value: Optional<T>
> 
> ```

I strongly support parameterized extensions.

And even when we have them, I still feel strongly that `extension Dictionary where Value: Optional` is a much more natural spelling.

The version with angle brackets is fine for the full-fledged feature that allows multiple parameters with constraints between them, but in the simple case the simple syntax should work as well.

Also, I don’t think this simple version needs to wait for the full-fledged feature. It is useful on its own, the syntax is clear, and perhaps the work to implement it might help lay the groundwork for more complex parameterized extensions.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 21, 2022, 3:50am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/6 "2022-01-21T03:50:08Z")

</div>

> [@Jumhyn](#):
>
> Notably [featured](https://belkadan.com/blog/2021/10/Swift-Regret-Generic-Parameters-are-not-Members/) in @jrose’s Swift Regrets series. 🙂

I'm certainly conflicted. On the one hand, I like that if you want them to be part of the API, you have to explicitly do so. On the other hand, a lot of folks would have motivation to use something other than `T` as a type parameter so that they don't have a bunch of `.T`s stuck on their types...

> [@Nevin](#):
>
> Yes it does.
> 
> The feature I am proposing lets you access to `Value.Wrapped` in that extension.

Thanks for clarifying; since there would be broader implications to introducing that syntax, we should be more explicit about it instead of leaving it as something implied by the surrounding idea. Is your suggestion that you'd _only_ be able to access the generic type using the `Value.Wrapped` syntax in an extension of this kind? What about contexts where someone would try to reference a generic type argument outside of one of these proposed extensions? For example,

```swift
// If this is allowed...
extension Array where Element: Optional {
  func foo() -> Element.Wrapped {}
}

// ...would this also be?
func foo() -> Optional<Int>.Wrapped {}

```

If the second one _is_ allowed, then we have consistency, but then this is now a much larger change than making a certain kind of extension possible; it adds new members to all generic types.

If the second one _isn't_ allowed, then we have an inconsistency where someone can access a member using a syntax and name inside an extension that they can't access using the same syntax and name outside of the extension, even when the actual types on the left-hand side are equivalent. That seems likely to cause confusion.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 4:01am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/7 "2022-01-21T04:01:29Z")

</div>

> [@allevato](#):
>
> ```swift
> // ...would this also be?
> func foo() -> Optional<Int>.Wrapped {}
> 
> ```

It’s mildly surprising that this doesn’t already work today.

Within `extension Optional` you can write `Self.Wrapped`, so the inconsistency you describe already exists today. There’s no fundamental conceptual difference between that and `Value.Wrapped` as proposed: one is an extension where `Self` is optional, and the other is an extension where `Self.Value` is optional.

I don’t think that concern about such an inconsistency is any reason to wait on what I’m proposing, but rather it is simply another reason to make `Wrapped` available on `Optional` in every context, as a separate and orthogonal feature.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [January 21, 2022, 4:29am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/8 "2022-01-21T04:29:02Z")

</div>

I think the discussion about making generic parameters into members is leading the discussion farther from the point that with the possible introduction of parametric extensions, the following extension context would not need to access `Wrapped` because it already has `T`:

> [@allevato](#):
>
> `extension <T> Dictionary where Value: Optional<T>`

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 21, 2022, 4:39am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/9 "2022-01-21T04:39:14Z")

</div>

> [@Nevin](#):
>
> Within `extension Optional` you can write `Self.Wrapped` , so the inconsistency you describe already exists today.

Do you have an example where that works? Because this is what I see:

```swift
$ swiftc -version
swift-driver version: 1.26.9 Apple Swift version 5.5.1 (swiftlang-1300.0.31.4 clang-1300.0.29.6)
Target: arm64-apple-macosx12.0

$ swiftc -c - <<'EOF'
extension Optional { func wrappedValue() -> Self.Wrapped { fatalError() } }
EOF

<stdin>:1:50: error: 'Wrapped' is not a member type of generic enum 'Swift.Optional<Wrapped>'
extension Optional { func wrappedValue() -> Self.Wrapped { fatalError() } }
                                            ~~~~ ^
Swift.Optional:1:21: note: 'Optional' declared here
@frozen public enum Optional<Wrapped> : ExpressibleByNilLiteral {
                    ^

```

If you're trying it in a context where you've already applied the `OptionalProtocol` conformance from your original post, that would explain it since the `associatedtype` would automatically get a `typealias` from the matching generic argument.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 4:47am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/10 "2022-01-21T04:47:46Z")

</div>

My mistake, you’re right. You can write `Wrapped` but not `Self.Wrapped`.

…which is itself surprising. Those seem like they ought to be synonyms there.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 21, 2022, 5:04am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/11 "2022-01-21T05:04:12Z")

</div>

> [@Nevin](#):
>
> My mistake, you’re right. You can write `Wrapped` but not `Self.Wrapped` .
> 
> …which is itself surprising. Those seem like they ought to be synonyms there.

It's a pretty fundamental difference between a generic parameter and a typealias member:

```swift
struct S<T> {
    typealias U = T
}
print(S<Int>.U.self) // Int
print(S<Int>.T.self) // Type 'S<Int>' has no member 'T'

```

As the author of the type, you could in fact design in such a way as to expose the generic parameter as a member:

```swift
struct Optional<Wrapped> {
    typealias Wrapped = Wrapped
}
print(Optional<Int>.Wrapped.self) // Int

```

But anyway, I think there's widespread consensus that parameterized extensions are a good thing, and that it would enable _a_ spelling for your use case. If a "more natural" spelling would either be inconsistent with other parts of the language or require a separate proposal to fundamentally revamp the language, then it's an open question whether in fact it's more natural and to whom and in what circumstances.

It seems to be leading us further away from the point that (a) yes, it'd be nice to have support for the use case you describe; and (b) it'd be supported by parameterized extensions which we all seem to very much support (and, for which, incidentally, an implementation likely doesn't need further groundwork-laying as there's been an open PR since 2019).

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 5:15am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/12 "2022-01-21T05:15:45Z")

</div>

> [@xwu](#):
>
> ```swift
> struct Optional<Wrapped> {
> typealias Wrapped = Wrapped
> }
> 
> ```

I trust you can see how absurd that line looks.

An alias for a type, which has the exact same name as the type it is aliasing, and that somehow behaves differently than not having the alias?

Plus it doesn’t even have to be in the original type declaration. Anyone can add that alias in an extension:

```swift
extension Optional {
  typealias Wrapped = Wrapped
}

```

So really, all the discussion about `Value.Wrapped` needing extra complexity, is rather superficial.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 21, 2022, 5:34am UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/13 "2022-01-21T05:34:37Z")

</div>

> [@Nevin](#):
>
> I trust you can see how absurd that line looks.
> 
> An alias for a type, which has the exact same name as the type it is aliasing, and that somehow behaves differently than not having the alias?

🤷‍♂️ As you say, it's an orthogonal topic, not necessary to resolve for your use case here to be expressible.

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [January 21, 2022, 8:19pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/14 "2022-01-21T20:19:36Z")

</div>

Another workaround for your specific example is:

```swift
extension Dictionary where Value: ExpressibleByNilLiteral {
  func compacted<NewValue>() -> [Key: NewValue] where Value == NewValue? {
    compactMapValues { $0 }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Zollerboy1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zollerboy1/32/12997_2.png) [@Zollerboy1](https://forums.swift.org/u/Zollerboy1)\
**Post date:** [January 21, 2022, 8:26pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/15 "2022-01-21T20:26:37Z")

</div>

Is that even needed? Can't you just do this:

```swift
extension Dictionary {
    func compacted<Wrapped>() -> [Key: Wrapped] where Value == Wrapped? {
        self.compactMapValues { $0 }
    }
}

```

?

For me that works as expected:

```swift
let dict: [Int: String?] = [
    0: "Hello",
    1: nil,
    2: "Foo"
]

print(dict.compacted()) // prints '[2: "Foo", 0: "Hello"]'

let dict2: [Int: String] = [:]

print(dict2.compacted()) // Error: Instance method 'compacted()' requires the types 'String' and 'Wrapped?' be equivalent

```

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [January 21, 2022, 8:40pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/16 "2022-01-21T20:40:46Z")

</div>

> [@Zollerboy1](#):
>
> Is that even needed?

The outer where clause will filter the method in code completion suggestions.

The inner where clauses are equivalent. `NewValue` and `Wrapped` are both generic parameters of the method.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 21, 2022, 8:54pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/17 "2022-01-21T20:54:18Z")

</div>

Right, the fact that there is already a protocol to which only `Optional` conforms, means the boilerplate already exists for that particular type.

But there’s no existing analog to the nil-literal protocol for other types. Even for `Dictionary`, there are multiple types expressible by dictionary literals. Heck, technically there’s even another type that conforms to `ExpressibleByNilLiteral`: our good friend `_OptionalNilComparisonType`.

And it’s true that making the method generic avoids needing the associated type in the protocol. But the method is not really generic. It’s fully constrained. Contorting the generic constraint system to make a fully constrained generic method is not ideal.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 25, 2022, 2:46pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/18 "2022-01-25T14:46:29Z")

</div>

Okay, I’ve gone back through the [Parameterized Extensions](https://forums.swift.org/t/parameterized-extensions/25563) proposal, and based on the examples shown there I am led to the conclusion that it amounts to, essentially, syntactic salt for what I propose here.

The motivation section of that proposal contains exactly 2 examples, one of which is essentially identical to my motivation here, and the other is pure sugar independent of both proposals and could be implemented as such today.

Going through that proposal section by section:

> **Motivation**
>
> > [@](#):
> >
> > # Parameterized Extensions
> > 
> > ## Motivation
> > 
> > Currently in Swift, one cannot give extensions generic parameters to build expressive generic signatures for new members. Consider the example:
> > 
> > ```swift
> > // You can't really express this extension where any array whose element is an optional type.
> > // This doesn't work.
> > extension Array where Element is Optional {
> > 
> > ```
> 
> This is the same motivation as for my thread here. The syntax they use to convey the idea is very similar to what I propose, except they have “`is`” in place of “`:`”.
> 
> > [@](#):
> >
> > ```swift
> > // error: constrained extension must be declared on the unspecialized generic
> > // type 'Pair' with constraints specified by a 'where' clause
> > extension Pair<Int> {
> > 
> > ```
> 
> This is pure sugar, not a parameterized extension. We could make this valid today by simply teaching the compiler to interpret it as `extension Pair where Element == Int`.
> 
> • • •

> **Proposed solution**
>
> > [@](#):
> >
> > ## Proposed solution
> > 
> > Extensions can now be decorated with a generic parameter list to be used with constructing a generic signature when extending types. Using the array of optionals example above, we can now write:
> > 
> > ```swift
> > extension<T> Array where Element == Optional<T> {
> > // ...
> > }
> > 
> > ```
> 
> This is the same functionality as I propose, but with a more awkward spelling. It is equivalent to my “`extension Array where Element: Optional`”.
> 
> > [@](#):
> >
> > ```swift
> > // Extend all arrays whose elements are optionals whose wrapped type conforms to FixedWidthInteger
> > extension<T: FixedWidthInteger> Array where Element == T? {
> > 
> > ```
> 
> As before, this is isomorphic to what I propose. There is no functional difference between the above and its equivalent in non-parameterized syntax:
> 
> ```swift
> extension Array where
> Element: Optional,
> Element.Wrapped: FixedWidthInteger
> {
> 
> ```
> 
> It is true that the `<T>` form is shorter, but at the same time the second version reads more naturally and better matches what we already write for protocol extensions with multiple constraints.
> 
> > [@](#):
> >
> > ### Extending types with same type requirements
> > 
> > ```swift
> > // Extend array whose element type is an optional T
> > extension<T> Array<T?> {}
> > 
> > ```
> 
> This is just sugar for the first example. Again it is shorter than “`extension Array where Element: Optional`”, but not necessarily as intuitive.
> 
> > [@](#):
> >
> > ### Extending specialized types
> > 
> > ```swift
> > // We don't need to know that Array's only generic parameter is named Element
> > // This is especially useful for types that have not so great generic parameter
> > // names, such as T, U, V, etc.
> > extension Array<String> {}
> > 
> > ```
> 
> This is the pure-sugar example again. It is independent of parameterized extensions.
> 
> > [@](#):
> >
> > ```swift
> > // Extend Array where the element type is an optional T
> > extension<T> [T?] {}
> > 
> > ```
> 
> This is just a sugared form of the ongoing example, which is still equivalent to my proposed “`extension Array where Element: Optional`”.
> 
> • • •

> **Detailed design**
>
> > [@](#):
> >
> > ## Banning generic parameters extensions
> > 
> > When one extends the generic parameter that was declared in the extension, the compiler will diagnose that it's currently unable to do so.
> > 
> > ```swift
> > extension<T> T {} // error: cannot extend non-nominal and non-structural type 'T'
> > 
> > ```
> 
> The parameterized extensions proposal needs a special-case to prohibit this, whereas with my proposal it simply follows from the existing restriction on extending `Any`.
> 
> > [@](#):
> >
> > ### Conditional Conformance
> > 
> > Parameterized extensions allow for some very neat generic signatures, including conditional conformance.
> > 
> > ```swift
> > // If Array's Element type is Equatable, conform to Equatable
> > extension<T: Equatable> [T]: Equatable {}
> > 
> > ```
> 
> This is isomorphic to what is already done today: `extension Array: Equatable where Element: Equatable`. I do not see any benefit to introducing a new, punctuation-dense syntax for what can already be written clearly with words.
> 
> • • •

> **ABI stability**
>
> > [@](#):
> >
> > ## Effect on ABI stability
> > 
> > Rewriting your conditional conformance to use parameterized extensions instead would break ABI.
> > 
> > ```swift
> > // Before
> > extension Array: Equatable where Element: Equatable {}
> > 
> > // After (ABI broke)
> > extension<T: Equatable> [T]: Equatable {}
> > 
> > ```
> 
> …and here we see an actual drawback to introducing the second parallel syntax. No such problem exists with my proposal, because there would remain only one way of writing this.
> 
> • • •

> **Future Directions**
>
> > [@](#):
> >
> > ## Future Directions
> > 
> > Right now, extending generic parameters are banned. One could extend a generic parameter to add members to all types in the future.
> > 
> > ```swift
> > // Extend every type to include a instance member named `abc`
> > extension<T> T {
> > func abc() {}
> > }
> > 
> > ```
> 
> If we were to decide to allow extensions of all types, the natural spelling would be `extension Any`. Once again, parameterized extensions would create a parallel syntax with no benefit.
> 
> > [@](#):
> >
> > ```swift
> > // Extend every type that conforms to ProtoA to conditionally conform to ProtoB as well.
> > extension<T: ProtoA> T: ProtoB {}
> > 
> > ```
> 
> This is a new and powerful feature. I think it is worth considering this functionality on its own merits. Notably, this is not actually part of the parameterized extensions proposal: it is a potential future direction.
> 
> If we decide we want this, then we can come up with a good spelling for it at that time. I do not think we should salt the syntax for using generic types as generic constraints, based on the mere possibility of a future feature.
> 
> > [@](#):
> >
> > ```swift
> > // Extend tuple to conform to Equatable where all of its Elements conform to Equatable
> > extension<...Elements: Equatable> (Elements...): Equtable {}
> > 
> > ```
> 
> This is covered by the separate [Variadic Generics](https://forums.swift.org/t/variadic-generics/54511) proposal.
> 
> • • •

My ultimate takeaway is that the actual motivating problem to be solved, is that generic types cannot currently be used as generic constraints.

If we could write

```swift
extension Dictionary where Value: Optional

```

and have access to `Value.Wrapped` within that extension, there would be no need for the parameterized extensions proposal. Its motivating use-case would be solved.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 25, 2022, 4:17pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/19 "2022-01-25T16:17:44Z")

</div>

> [@Nevin](#):
>
> If we could write
> 
> ```swift
> extension Dictionary where Value: Optional
> 
> ```
> 
> and have access to `Value.Wrapped` within that extension, there would be no need for the parameterized extensions proposal. Its motivating use-case would be solved.

By the same token, then, parameterized extensions would solve your motivating use case, and then there would be no need to make a fundamental change in the language that makes generic parameters available as typealiases.

Regarding the future directions:

> [@Nevin](#):
>
> This is a new and powerful feature. I think it is worth considering this functionality on its own merits. Notably, this is not actually part of the parameterized extensions proposal: it is a potential future direction.
> 
> If we decide we want this, then we can come up with a good spelling for it at that time.

One sign of a good design is that it makes future directions natural rather than awkward warts.

In the ideal case, parameterized extensions would in even their first iteration cover the use case “extend every type where …” and not just “extend every X where …”. However, that they are divisible features with a natural resting place in the language that supports the latter and not the former does _not_ mean that an ideal design has two different spellings that treats these use cases as unrelated. Indeed, as you point out, the _syntax_ of parameterized extensions makes the former feature expressible without anything else added to the language—it’s just that the proposal (as it was then pitched) doesn’t encompass the implementation work to make it actually compile, so has to ban it.

It’s a powerful argument—certainly at least persuasive for me—in favor of tackling your use case as a specific use of parameterized extensions rather than as its own topic.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 25, 2022, 4:28pm UTC](https://forums.swift.org/t/generic-type-as-generic-constraint/54765/20 "2022-01-25T16:28:22Z")

</div>

Conversely, we should make generic parameters available anyway, for the reasons Jordan Rose outlined, and we should make the syntax I propose work for consistency and simplicity.

Once we have those two things, then parameterized extensions as proposed only serve to introduce a second, redundant. punctuation-heavy spelling for the same thing, and that redundant syntax creates an attractive-nuisance for unintentionally breaking ABI.

[Next page](https://forums.swift.org/t/generic-type-as-generic-constraint/54765.md?page=2)
