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

kotlin 'when' conditional operator equivalent in dart #1509

Closed
karthickr95 opened this issue Jan 21, 2019 · 6 comments
Closed

kotlin 'when' conditional operator equivalent in dart #1509

karthickr95 opened this issue Jan 21, 2019 · 6 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@karthickr95
Copy link

Need a kotlin like 'when' conditional operator.

Need a optional break statement and when like concise syntax, because it is less boilerplate and like a modern language. And a when or switch expression statement.

@lrhn
Copy link
Member

lrhn commented Jan 22, 2019

The Kotlin when statement is similar to a switch statement, but with more powerful patterns:

when (e) {
  (computedExpression) -> /* equality test with non-constant value */
  99, 100, 101 -> /* multiple patterns */
  in 10..25 -> /*range pattern */
  !in 10..20 -> /* not in range */
  is String -> /* type match */
  !is String -> /* negative type match */
  x.isEven -> /* boolean test */
}

It has implicit break between cases (no fallthrough), and allows a variable declaration in the switch expression:

when (var x = something) {
  is String -> print(x) 
  else -> print("$x")
}

which is in scope in the rest of the when.

A when can occur as an expression, in which case it likely requires an else branch. The value of the when is the value of the last expression in the chosen branch.

@djensen47
Copy link

It would also be great to make the result of the when clause assignable to a variable, return, etc.

@mit-mit mit-mit transferred this issue from dart-lang/sdk Mar 11, 2021
@mit-mit
Copy link
Member

mit-mit commented Mar 11, 2021

Moving to the /langauge repo, which is the main tracker for language evolution

@rodrigost23
Copy link

Isn't this a duplicate of #27?

@mit-mit
Copy link
Member

mit-mit commented Mar 11, 2021

Yes, that appears to be the case, @munificent ?

(Part of the reason for moving was to ensure things weren't duplicated, so thanks for the headsup @rodrigost23 )

@munificent
Copy link
Member

It's always hard to decide which language issues are duplicates. If the request is just some kind of multi-way branching expression, then, yeah. But if @karthickr95 is specifically requesting syntax very close to Kotlin's then maybe not. In this case, though, I think it's similar enough to close as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

6 participants