-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Switch Expression Syntax with Block #2964
Comments
You can't, basically.
If you just want to return 1, you can just write _ => 1
If you want to set the variable r as well as Value, but only if input isn't
0, you can't. Even if that was valid, r would only have scope within the
block, and wouldn't carry on past the switch expression.
…On Fri, 15 Nov 2019, 14:15 Tony Valenti, ***@***.***> wrote:
Hi All,
How do I do the following in a switch expression?
var Value = input switch {
0 => 0,
//How do I do this part?
_ => {
var r = 1;
return r;
}
};
Thanks so much!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2964?email_source=notifications&email_token=ADIEDQJGWTRQM3A4RC6TXVDQT2VG7A5CNFSM4JN3LRAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HZT374A>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQJLR3WWBSRRC4OHIBDQT2VG7ANCNFSM4JN3LRAA>
.
|
Interesting! Based on the Am I right? |
No, you're not allowed blocks; only simple expressions.
If you need to do some calculation work and then return a single value, you
can create a local function and use that as the expression to return, but
that still wouldn't be able to set a second variable.
…On Fri, 15 Nov 2019, 15:44 Tony Valenti, ***@***.***> wrote:
Interesting! Based on the => syntax, I really perceived that what was on
the right hand was an anonymous function which would allow block style
syntax. From what I'm hearing, that is not correct though.
Am I right?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2964?email_source=notifications&email_token=ADIEDQK4DZ5B4BE4FI3ET63QT27U3A5CNFSM4JN3LRAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEF2HMA#issuecomment-554410928>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQLAKJZWDIKCXWN2KH3QT27U3ANCNFSM4JN3LRAA>
.
|
There are some proposals to explore allowing multiple statements to act as an expression: |
Either #377 or define a local function and call it. |
Closing as championed at #3037 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi All,
How do I do the following in a switch expression?
Thanks so much!
The text was updated successfully, but these errors were encountered: