Variable with generic protocol type


(Rtnm Nosurname) #1

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?


(Ross O'Brien) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution