Probably a compiler bug: can't convert Range<Int> to Sequence where Element == Int

Generic parameters allow you to write functions that accept or return any type that satisfies some constraints. The important thing is that the caller of a generic function decides what concrete type to use for the generic parameter.

In your case, the implementor of the function (you) wants to tell the caller that they can expect some Sequence with Int elements, but you don't want to allow them to specify which type of sequence they get.

AnySequence<Int> expresses exactly what you want to say. I suggest you go with it unless you observe a performance problem. (By using AnySequence, you're still specifying a generic parameter (the <Int> part), but this time you're the caller of the AnySequence initializer, telling it which element type to use.)

Here are two recent threads that might interest you:

3 Likes