I want to write a function that accepts an argument that is a Sequence of Strings (not necessarily an array of Strings, but anything that implements the Sequence protocol where the items in the sequence are Strings).
Actually, I'd like to pass in the keys to a dictionary that is keyed by strings (preferably, without creating a temporary array).
I can't seem to find the right syntax to specify that type for the argument.
CodePudding user response:
func doSomething<S: Sequence>(with seq: S) where S.Element == String
Or as per suggestion in comments by @LeoDabus you could include also substrings and other types conforming to StringProtocol by constraining the sequence’s element:
func doSomething<S: Sequence>(with seq: S) where S.Element: StringProtocol