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

Permit variable declaration under "or patterns". #3740

Closed
Happypig375 opened this issue Jul 28, 2020 · 4 comments
Closed

Permit variable declaration under "or patterns". #3740

Happypig375 opened this issue Jul 28, 2020 · 4 comments

Comments

@Happypig375
Copy link
Member

According to https://github.com/dotnet/csharplang/blob/6901635c383801e4d177085587aaccadaa7b2f11/proposals/patterns3.md#variable-definitions-and-definite-assignment, we will not be able to declare pattern variables under "or patterns".
Obviously this should be supported. We should at least allow cases where all branches of the "or pattern" declare the same number of variables of the same types. (For variables of different types, we should wait for #399.)

An example that this can simplify is

https://github.com/verybadcat/CSharpMath/blob/b8a5ab0ff9789627edf2f05927291a87631b29a0/CSharpMath.Evaluation/Evaluation.cs#L395-L403

The when clause can be simplified by #3435, but we still need to permit variable declaration under "or patterns" to merge the two cases into one and eliminate the unnecessary goto.

@jnm2
Copy link
Contributor

jnm2 commented Jul 28, 2020

I'd like the ability to declare variables on either side of || too:

return value is TInner inner || innerConverter.ConvertFrom(context, culture, value) is TInner inner
    ? Wrap(inner)
    : base.ConvertFrom(context, culture, value);

@huoyaoyuan
Copy link
Member

I'd like the ability to declare variables on either side of || too:

return value is TInner inner || innerConverter.ConvertFrom(context, culture, value) is TInner inner
    ? Wrap(inner)
    : base.ConvertFrom(context, culture, value);
((value as TInner) ?? (innerConverter.ConvertFrom(context, culture, value) as TInner)) is {} inner

Well you do have an alternative, and I'd say they are both ugly syntax.

@jnm2
Copy link
Contributor

jnm2 commented Jul 28, 2020

I can't use as because TInner is not constrained to class.

@YairHalberstadt
Copy link
Contributor

Closing as championed at #4018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants