-
Notifications
You must be signed in to change notification settings - Fork 4.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
Proposal: Better pattern matching with generic types #16156
Comments
Look at #6739, specifically the use of enums as algebraic data types. The Assuming that syntax is adopted as-is, you'd probably work with it as follows: public enum class Result<T> {
Success(T result),
Error(Exception error)
}
Result<T> result = ...; // calculate some result here
if (result is Success(var value)) { ... }
else if (result is Error(var error)) { ... } |
Is the pattern matching with generic types coming included with it? |
That proposal won't be implemented for C# 7.0, which is more or less feature complete now. I expect that pattern matching will be expanded upon over several releases of the language, hopefully more point releases than new versions. |
@gulshan Can you please show what kind of code the compiler would translate this into? |
I keep seeing the title and thinking "ooh, genetic types, what's that?" |
@gafter I'm not sure if this can be done with code generation. Now I realize there may be several steps for this proposal-
|
The "bare" generic type for // all legal and different types
public class Success { }
public class Success<T> { }
public class Success<T1, T2> { } For the C# compiler to assume that |
Then it can be |
@jnm2 Genetic types are great for inheritance. |
Discussion is on- dotnet/csharplang#268 |
Predecessor: #5023
Actually in the parent issue, pattern matching was not highlighted. Hence this new issue. Given these classes-
And this static method-
I want to write
instead of
Another option can be discarding the type with '_'-
The text was updated successfully, but these errors were encountered: