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

Idiomatic C# - using switch for pattern matching: #262

Closed
FreeApophis opened this issue Jan 11, 2021 · 9 comments
Closed

Idiomatic C# - using switch for pattern matching: #262

FreeApophis opened this issue Jan 11, 2021 · 9 comments
Labels
to discuss There are open points which need to be discussed

Comments

@FreeApophis
Copy link
Member

I like the idea to do pattern matching with the switch expression instead of our Match function:

louthy/language-ext#838

@FreeApophis FreeApophis added the to discuss There are open points which need to be discussed label Jan 11, 2021
@bash
Copy link
Member

bash commented Jan 11, 2021

.Case is essentially .ToNullable(), right?

@FreeApophis
Copy link
Member Author

At least for the Option type.
Don't know how much sense it makes to Either, especially becuase of exhaustiveness...
The Sequence use case with Head and Tail is done with tuple pattern matching, we don't have those.

I think I want to see an implementation to see the problems.

@bash
Copy link
Member

bash commented Jan 11, 2021

I'd really like to see something like dotnet/csharplang#1047 implemented 😕…

On a somewhat related note: We could make TryGetValue public. (I know, I know, but it slightly better than Case because it avoids boxing)

@FreeApophis
Copy link
Member Author

I am not very fond of matching true and false, but positional pattern sound great.

            var something = t.TryGetValue(out var value) switch
            {
                true => value,
                false => -1,
            };

@bash
Copy link
Member

bash commented Jan 12, 2021

Well you could use a ternary expression with TryGetValue:

var something = t.TryGetValue(out var value) ? value : -1;

@bash
Copy link
Member

bash commented Feb 22, 2021

Making TryGetValue public also enables the use of Option in situations where you're forced to do things imperatively. (e.g. exception handling)

Option<IExceptionHandler> FindExceptionHandler(Exception exception) => ...;

try
{
    ...
} catch (Exception exception) when (FindExceptionHandler(exception).TryGetValue(out var handler))
{
    handler.HandleException(exception);
}

@FreeApophis
Copy link
Member Author

lang-ext goes the other way, and defines a functional way to Try.

@bash
Copy link
Member

bash commented Feb 22, 2021

As long as it doesn't start polluting the stack trace too much…

@bash
Copy link
Member

bash commented Feb 24, 2021

I've created a new issue for TryGetValue: #296

@bash bash closed this as completed Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to discuss There are open points which need to be discussed
Projects
None yet
Development

No branches or pull requests

2 participants