Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14306] where clauses should allow optional bindings #56665

Open
lorentey opened this issue Mar 5, 2021 · 5 comments
Open

[SR-14306] where clauses should allow optional bindings #56665

lorentey opened this issue Mar 5, 2021 · 5 comments
Labels
compiler The Swift compiler in itself new feature

Comments

@lorentey
Copy link
Member

lorentey commented Mar 5, 2021

Previous ID SR-14306
Radar rdar://problem/75108188
Original Reporter @lorentey
Type New Feature
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels New Feature
Assignee None
Priority Medium

md5: 9b40370f10a483df62c6419520bab15d

Issue Description:

(From https://twitter.com/cocoaphony/status/1367547458507702273)

It ought to be possible to do this:

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.)

@lorentey
Copy link
Member Author

lorentey commented Mar 5, 2021

@swift-cicreate

@typesanitizer
Copy link

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?

@typesanitizer
Copy link

Arguably, there should be a matching where case let form too; to mirror the fact that we support both if case let and if let.

@swift-ci
Copy link
Collaborator

Comment by Owen Voorhees (JIRA)

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

enum Foo {
  case bar(Int)
  case baz(Int, String)
}

let a: String? = nil
let b = Foo.bar(42)

switch b {
  case .bar(let first) where let second = a,
  case .baz(let first, let second):
    print(first, second)
}

@kavon
Copy link
Contributor

kavon commented Mar 19, 2021

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.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself new feature
Projects
None yet
Development

No branches or pull requests

4 participants