You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func foo(type: String, value: Any?) {
switch type {
case "simple": print("simple")
case "int" where let value = value as? Int:
print("int \(value)")
case "int" where let value = value as? Int32:
print("int \(value)")
case "int" where let value = value as? Int8:
print("int \(value)")
case "str" where let value = value as? String:
print("str '\(value)'")
default:
print("other")
}
}
Currently this is a syntax error.
(This can always be worked around by e.g. switching on `value` in addition to `type`. Still, it would be nice to support this – e.g., consider when `value` is extracted from the payload of the corresponding enum case in a way that doesn't support pattern matching.)
The text was updated successfully, but these errors were encountered:
Should this be marked as a feature request or enhancement instead of a bug? I don't think we support where clauses with binding forms (in any place in the grammar) today, do we?
Just a note for anyone looking into this: we'd need to decide if something like the following should be allowed, which complicates things a bit. There's also a potential ambiguity if multiple statement condition elements are allowed
enumFoo {
casebar(Int)
casebaz(Int, String)
}
leta: String? = nilletb = Foo.bar(42)
switchb {
case .bar(letfirst) whereletsecond = a,
case .baz(letfirst, letsecond):
print(first, second)
}
I think this is more of a Swift Evolution kind of feature request that's looking for a champion, so I recategorized it under the "New Feature" category.
Additional Detail from JIRA
md5: 9b40370f10a483df62c6419520bab15d
Issue Description:
(From https://twitter.com/cocoaphony/status/1367547458507702273)
It ought to be possible to do this:
Currently this is a syntax error.
(This can always be worked around by e.g. switching on `value` in addition to `type`. Still, it would be nice to support this – e.g., consider when `value` is extracted from the payload of the corresponding enum case in a way that doesn't support pattern matching.)
The text was updated successfully, but these errors were encountered: