Generic type as generic constraint

Okay, I’ve gone back through the Parameterized Extensions 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

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 “:”.

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

This is the same functionality as I propose, but with a more awkward spelling. It is equivalent to my “extension Array where Element: Optional”.

As before, this is isomorphic to what I propose. There is no functional difference between the above and its equivalent in non-parameterized syntax:

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.

This is just sugar for the first example. Again it is shorter than “extension Array where Element: Optional”, but not necessarily as intuitive.

This is the pure-sugar example again. It is independent of parameterized extensions.

This is just a sugared form of the ongoing example, which is still equivalent to my proposed “extension Array where Element: Optional”.

• • •

Detailed design

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.

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

…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

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.

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.

This is covered by the separate Variadic Generics 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

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.

5 Likes