[swift-evolution] Variable with generic protocol type

Ross O'Brien narrativium+swift at gmail.com
Fri Jun 16 11:33:24 CDT 2017


As I understand it, and someone please correct me if I'm wrong, you've just
described *existentials*, which Swift doesn't fully support yet but which
are on the Generics Manifesto.

It would be nice to declare or cast a variable as 'conforms to protocol X
with associated type known to be Y' or 'conforms to protocol X with
associated type known to conform to Z'.

I don't know what syntax has been suggested / agreed upon for either of
these, though.

On Fri, Jun 16, 2017 at 3:13 PM, Rtnm Nosurname via swift-evolution <
swift-evolution at swift.org> wrote:

> Hello.
>
> Sorry, but C# code:
>
>     interface IValidator<T> {
>         bool validate(T value);
>     }
>     IValidator<String> validator;
>
> Easy, simple and clear.
>
> Swift:
>
>     protocol PValidator {
>         associatedtype ValidateType
>         func validate(value: ValidateType) -> Bool
>     }
>     var validator: ????
>
> it can be so:
>    var validator: PValidator where ValidateType == String
>
> or
>    var validator: <T: PValidator> where ValidateType == String
>
> or
>   var validator: PValidator<ValidateType == String>
>
> or
>    typealias StringValidator = PValidator<ValidateType == String>
>    var validator: StringValidator
>
> No matter how, but it should be.
>
> What alternatives offers swift?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170616/45419925/attachment.html>


More information about the swift-evolution mailing list