protocol myProtocol {}
func doSomething(with params: (some myProtocol)...) {
// Implementation goes here
}
extension Int: myProtocol {}
doSomething(with: 1, 2, 3)
Compilation error at the func declaration line:
'some' types are only permitted in properties, subscripts, and functions
I could simply omit the keyword altogether, but then it is considered to be 'any' by default. Consequently, I cannot pass the params to a function that expects [some SyntaxProtocol] as a parameter.
Why you can't use
somein variadic parametersDisallowing usage of opaque types(
some Protocol) in variadic parameters was a conscious decision by the language developers. Because it would conflict with another proposed language feature calledvariadic generics:Source: https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md#variadic-generics
So, it's not some bug, or oversight. It's working as intended.
What is the meaning of that
somekeyword in parameter type declaration anyways?But what should you do with your code? Well, we gotta check why that language feature was added in the first place, and what it is doing "under the hood":
And it was introduced to make function with heave usage of generics "lighter" and easier to read.
To turn code like:
into this:
Both of those snippets are equivalent, version with
some Viewduring compilation just turns into generic function version.Source: https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md#proposed-solution
What should you do
As it was said before: this synctactic sugar is explicitly unavailable for variadic parameters. So, only obvious solution would be to fall back to the "unsugared" syntax and use generic parameters:
And it will work as intended (if you intended for that all parameters in variadic list should have exactly the same type, that confirms to
myProtocol, ofc).