Constraining associated type in extension

Hi swift users! I have some code that is generic over StringProtocol, but only when removeFirst(_ k: Int) is available, so constraint is T.SubSequence == T:

struct Foo<T> {}
func foo<T: StringProtocol>(_ s: String) -> Foo<T> where T.SubSequence == T {...}

Now it is so happens that it would be very useful to conform ExpressibleByStringLiteral in that situation, but, unfortunately, compiler is not happy with that:

error: 'Subsequence' is not a member type of 'T'
extension Foo: ExpressibleByStringLiteral where T: StringProtocol, T.Subsequence == T {}
                                                                   ~ ^

Is there any workarounds for this?

You have a typo: It's SubSequence, not Subsequence

3 Likes

omg, :man_facepalming:, thank you very much. That Substring/SubSequence duality gonna kill me someday :upside_down_face: