Questions about the future of Protocols with Associated Types

Because they wouldn't do what you want to do here. If we supported generic protocols, your type wouldn't conform to Predicate; it would conform to Predicate<Int> or Predicate<String> or whatever, and it could conform to both at the same time. This is the same as how, if you have a generic class MyClass, you don't write class Subclass: MyClass; you write class Subclass<T>: MyClass<T>.

What I think you actually want here is the generalized existential feature. "Generalized existential" is a ten-dollar term for a ten-cent idea: "Swift should have built-in type-erasing wrappers for PATs, much like it has them for ordinary protocols." Their syntax might be slightly different—they'd probably use a where clause instead of a generic parameter list—but they would do the thing you want to do here.

We probably won't get the kind of generic protocols I described; they're a little too niche. But there is a ton of demand for generalized existentials and I think we're planning to do them eventually.

I very much hope we aren't screwed and stuck.

7 Likes