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

Question: could/will patttern matching expressions support void results? #9717

Closed
DavidArno opened this issue Mar 12, 2016 · 10 comments
Closed
Labels

Comments

@DavidArno
Copy link

Currently - as I understand it - it is proposed that switch be extended to support pattern matching and that a new keyword match by added to support pattern matching expressions. Assuming a Option<T> union, their respective syntaxes might be:

void OptionValue<T>(Option<T> option) =>
    switch (option)
    {
        case Some(var value):
            Console.WriteLIne($"Value is {value}");
            break;
        case None:
            Console.WriteLIne("No value");
            break;
    };

bool HasValue<T>(Option<T> option) =>
    option match (
        case Option(*) : true,
        case None: false
    );

However, Roslyn already supports, for example, InvocationExpression and SimpleAssignmentExpression for void expression bodies, such as

void WriteX<T>(T x) => Console.WriteLine(x);

The notion of statements being expressions and vice versa is already baked into the language. Thus it ought to be possible to to treat match similarly, such that it can be a void statement or expression:

void OptionValue<T>(Option<T> option) =>
    option match (
        case Some(var value): Console.WriteLIne($"Value is {value}"),
        case None: Console.WriteLIne("No value")
    );

Will this be possible, and if so, is it planned for the pattern match expression supporting release of C#?

@HaloFour
Copy link

#8821 (comment)

@DavidArno
Copy link
Author

@HaloFour,

Yes I'm aware of @grafter's opinion on the matter. I feel his position is ill-thought out though given that void expressions are already supported for other language features via InvocationExpression. The pattern-matched switch syntax is a train-wreck of a new feature, so I see no logical reason not to support void match.

@HaloFour
Copy link

@DavidArno So you've already made your argument and at least one member of the LDM team has weighed in. I don't see what repeating that argument will buy you considering that you haven't added anything new. The team clearly has a different idea as to the direction of the implementation of pattern matching in C# than you do. You're welcome to your opinion that this is a train wreck. I'm not sure that constantly voicing this will do much to improve your argument.

@alrz
Copy link
Member

alrz commented Mar 14, 2016

The only expression that might return void is an invocation-expression (naturally), the fact that you are using it in a match (or a ternary) doesn't make them return void.

Your example could be written like this:

void OptionValue<T>(Option<T> option) =>
    option is Some(var value) ? Console.WriteLIne($"Value is {value}")
        : Console.WriteLIne("No value");

Which won't compile either. Same is true for match because C# is not an expression-based language — statements and expressions have distinct semantics.

@DavidArno
Copy link
Author

@alrz,

InvocationExpression is used for wrapping void statements in an expression and I assume was created for void statements in expression-bodied methods. I see no reason, other than "we don't want to" for why match can't be parsed to an InvocationExpression if that expression is void. Do you of another reason?

@alrz
Copy link
Member

alrz commented Mar 14, 2016

InvocationExpression is used for wrapping void statements in an expression

It doesn't wrap anything, it is the production rule for a method invocation.

I assume was created for void statements in expression-bodied methods.

It was there since version 1.0.

why match can't be parsed to an InvocationExpression

Because it's not an invocation expression. I think you are misunderstanding C# grammar.

@dsaf
Copy link

dsaf commented Mar 14, 2016

Maybe there could be a standard Unit type to cater to more functional C# coding style?

https://msdn.microsoft.com/en-us/library/system.reactive.unit(v=vs.103).aspx

Represents void.

http://www.introtorx.com/content/v1.0.10621.0/04_CreatingObservableSequences.html

If you use the overload that takes an Action, then the returned sequence will be of type IObservable. The Unit type is a functional programming construct and is analogous to void. In this case Unit is used to publish an acknowledgement that the Action is complete, however this is rather inconsequential as the sequence is immediately completed straight after Unit anyway. The Unit type itself has no value; it just serves as an empty payload for the OnNext notification.

Inspired by F# I guess:

https://msdn.microsoft.com/en-us/library/dd483472.aspx

The unit type is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed.

@dsaf
Copy link

dsaf commented Mar 14, 2016

It would of course lead to proliferation of libraries that would not provide Action<> overloads at all thus breaking the language ideomacy.

@DavidArno
Copy link
Author

@alrz,

I had assumed that InvocationExpression was a way of mapping statements to expressions, but the clue was in the name. I've updated the original comment to reflect what I was talking about therefore. Thanks.

@gafter
Copy link
Member

gafter commented Mar 21, 2016

The current specification for the pattern-matching expression uses specification terminology similar to the terminology describing the result type of ?:, and does not support void subexpressions for the same reason (there is no conversion to or from the void type).

@gafter gafter added Question Resolution-Answered The question has been answered labels Mar 21, 2016
@gafter gafter closed this as completed Mar 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants