protocol Pro {}
struct Base: Pro {}
let x: any Pro = Base()
struct ST {
static var val: any Pro? // 'any' has no effect on concrete type 'Pro?'
static func set(_ val: any Pro) {
self.val = val
}
}
I got this error:
/home/carlhung/swiftProjects/playground/Sources/playground/main.swift:21:21: error: 'any' has no effect on concrete type 'Pro?'
static var val: any Pro? // 'any' has no effect on concrete type 'Pro?'
The way that any binds to the type coming after it, any Pro? is parsed as any Optional<Pro>, and Optional<Pro> is a concrete type containing any Pro value or nil. Instead, if you want to optionally store any Pro, you'll need to wrap any Pro in parentheses: static var val: (any Pro)?
This error should come with a fix-it and a more customized message, I think. Unfortunately, at least during this transition phase where P? is accepted, any P? is taken to be a synonym for any (any P)? and the error is particularly confusing.
Recalling that we debated whether we should make any P? do the "intuitive" thing or whether we should aim for consistency in precedence, I think users are likely to have this question both during this transition phase and after bare P? is no longer accepted, so any diagnostic QoL improvements here are going to be worthwhile. But certainly today, while any is new and the error is confusing, it'd be really helpful to give users good guidance here.
I fully agree. I was trying to add some any today, and I could not solve the Optional puzzle. I just thought the feature was incompletely implemented, and moved on (since any is not required).
On the other way, the proposal was perfectly clear for meta types, so it helped me solving the any P.Type vs (any P).Type puzzle.
We just need explicit help for optionals.
I just thought the feature was incompletely implemented
This should raise an alarm to people in charge. There are more and more holes, and it's difficult to know why some syntax won't work as expected. It may be a programmer error, or just the sign that language developers did not finish their job. There are more and more signs that the language is stuck in a beta phase.
Because as I heard in swift 6. It is going to have an error when using existing type without marking “any”. So, it better adopts on my current as soon as possible. Reduce fixing it in the future.
I completely agree that all errors about any should come with a fix-it (whether that's to remove the any or add parenthesis where appropriate). There's been a lot of active work on diagnostics QoL for any:
I've also recieved a lot of feedback already about (any P)? and I'm completely open to amending the proposal to make any P? just work the way everyone expects it to work.
IMO, we should make sure that some P? and any P? are in sync with respect to this. The same intuitions apply to both, and there should be no reason that switching from an existential to an opaque type or vice versa occasions addition of parens.