As we prepare our Codebase toward Swift 6 we also started to explicit mark existentials with any. Though not a requirement for Swift 6.0 anymore we kind of got used to it. When it comes to optional existential it's a different story. Every time Xcodes autocompletes a protocol requirement like
func showOrderDetails(delegate: (any OrderDetailsDelegate)?) I screw up my face and remove the any along with the parenthesis. Why are they necessary? There does not seem to be an alternate meaning for any OrderDetailsDelegate? for readability I would much prefer it without the parenthesis. I looked through the different existential proposals, but couldn't find an explicit explanation on the optional syntax.

I apologize if that has been discussed before.

1 Like

? is just a syntax sugar for Optional<T>. any and some are part of the type, so parentheses make it clear that this is equivalent to Optional<any P> and not any Optional<P>, with the latter have no meaning for the type system.

That is a valid future direction. It would just need a proposal and implementation.

3 Likes

I ran into something similar looking for a way to express:

some OrderDetailsDelegate.Type

The right way (for now) to do this is:

(some OrderDetailsDelegate).Type

But in theory Swift could one day compile that without the round braces (unless there is actually a legit use-case I'm not thinking of for that syntax without round braces that currently means something distinct that needs to be supported).

There is a distinction between (any P).type and any P.type, which are not equivalent. The distinction is already not easy to explain, but a shorthand where (some P).Type means some P.Type would not help matters.

3 Likes