How can I restrict the Swift protocol to a specific type?

Take a look at the following hypothetical code:

class Stream<S: SequenceType where S.Generator.Element: Character> { init(_ sequence: S) {} } 

Not compiled. I get "S.Generator.Element limited to non-protocol type". This is a bummer, man. I thought of two possibilities:

 class Stream<S: SequenceType where S.Generator.Element: ExtendedGraphemeClusterLiteralType> { } 

This limitation works because Character is the only thing I know for implementing this protocol. The problem is that I now have ExtendedGraphemeClusterLiteralType instead of Character , so I am forced to drop with which I can live.

Another possibility is only to define your own protocol, for example CharacterType , and implement Character through the extension. (This is probably also safer.) This is probably the approach I will actually take, but I wondered if anyone knew of this limitation other than this?

+7
generics swift
source share
1 answer

Try:

 class Stream<S: SequenceType where S.Generator.Element == Character> { // ^^ init(_ sequence: S) {} } 
+12
source share

All Articles