Bikeshedding on Error messages for SR-2022

Bug SR-2022 suggests improving the error message for using as? in a case/let where a plain as (no question mark) should have been used.

The current the errors are

  switch myVariable {
    case let n as? Float:

...

SR-2022.swift:13:13: warning: cast from '_' to unrelated type 'Float' always fails
    case let n as? Float:
             ~ ^   ~~~~~
SR-2022.swift:13:13: warning: cast from '_' to unrelated type 'Float' always fails
    case let n as? Float:
             ~ ^   ~~~~~
SR-2022.swift:13:13: error: expression pattern of type 'Float?' cannot match values of type 'Any'
    case let n as? Float:
             ~~^~~~~~~~~

The fix is to remove the question mark, which is not suggested.
So there is room for improvement here.

My question for everyone here is: what should the error message be?

  1. 'as?' cannot appear in switch pattern bindings!
  2. Lose the question mark meatbag.
  3. 'as' in 'case' statement dose not use '?'
  4. don't use as?!
  5. something else?
Terms of Service

Privacy Policy

Cookie Policy