-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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: MatchFailureException #27832
Comments
It may be useful to provide more context with an example of "when no branch of a pattern-matching switch expression matches the input at runtime." |
Should the exception include a constructor that takes a |
@svick Like with |
I've added an (optional) |
I changed the base type to |
It should also override GetObjectData (and maybe ToString). |
@stephentoub I was proposing the API, not the implementation. |
@gafter, it actually is API (at least from the perspective of what's in a ref assembly). The type isn't sealed, so if those aren't exposed in the API but they're there in the implementation, a derived type doing "base." to invoke them may end up skipping the implementation and going to the next ancestor in the hierarchy. |
With the current proposal, if |
e.g. if it's something that can't be boxed, like a |
If only we had |
Updated to add |
We had a quick look today. It looks we should have a chat with the C# folks to better understand the scenario. Why do we need a new exception? Can't we just throw an existing one, such as:
One of the reasons to not require a new exception type is that can (trivially) support this feature on all existing .NET platforms without any problems. It seems (at least to me) that he |
FWIW, I pushed for this to be included because it can only help with debugging, and in most cases it should be able to include the value (it won't in corner cases like matching on a ref struct). The value's ToString would be output as part of Exception's ToString, similar to how it is for ArgumentOutOfRangeException. |
I think in most cases developers would not have to check the source code, as long as there's a property called HasUnmatchedValue or an equivalent way of knowing that the unmatched value is present (and therefore it's actually null, not just a missing piece of information). |
There is no existing exception whose meaning is close enough for our purposes:
Most other systems and languages that have pattern-matching use a designated exception for this situation. Our current plan does not have a problem running on older platforms, but at the loss of a meaningful exception on such platforms - you get the not quite appropriate |
@gafter I assume that once this type is added for Core 3, you'll want it ported to mono as well. |
That seems like a bad idea as that's a source breaking change:
Are we sure we want behavior mismatches like this? FWIW, I think either of the existing exception APIs is good enough. Personally, I think My only concern is that this leads to source breaking changes. |
@terrajobst What do you mean by source breaking changes? If your program crashes on .NET Core 2.x, it will crash on .NET Core 3.x. If you catch the excepetion (using the type What is the source breaking change you're concerned about? |
Would it be confusing to have an exception named |
Ah, I missed the inheritance relationship. That addresses my concern. |
What about |
Why would "match" imply regex? Is that a regex-specific word? |
No, but it's a common use of it. For example our public type named
I think that's better (although |
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that a switch expression that was non-exhaustive failed to match its input
/// at runtime, e.g. in the C# 8 expression <code>3 switch { 4 => 5 }</code>.
/// The exception optionally contains an object representing the unmatched value.
/// </summary>
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public sealed class SwitchExpressionException : InvalidOperationException
{
public SwitchExpressionException();
public SwitchExpressionException(object unmatchedValue);
public object UnmatchedValue { get; }
[System.Security.SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context);
}
} |
So how do we know whether |
The current shape of the API does not provide a way for someone catching it to know if an unmatched value is present. However, it is not intended for programmatic use. |
But it's important for debugging. If I see this exception and see that |
Since this is not intended to ever be caught and accessed by user code, could |
@terrajobst The name of this exception is now tied to the name of the construct in C#. If we add a similar construct in VB, it probably won't be called a switch expression, and reusing this exception would seem like a stretch. |
@maryamariyan can you please add this new exception next week? Looks like it's super easy, API is approved, basically no code (like eg @gafter @terrajobst are |
@terrajobst can you confirm whether it's SwitchExpressionEXception as reviewed or MatchFailureException as @gafter requests just above. |
It is |
@gafter OK thanks, I was confused by your subsequent comment suggesting you disagreed. https://github.com/dotnet/corefx/issues/33284#issuecomment-448385260 |
* Add SwitchExpressionException Fixes: #33284 * Apply PR feedbacks * Type just added to .net core 3.0 * quick cleanup * Add test in BinaryFormatterTestData * Updated the APIs for SwitchExpressionException * Remove IsSerializable condition
@gafter this is in please feel free to wire it up! |
The compiler is all wired up in the latest VS2019 preview, though I haven't tried it with the latest Core. |
Rationale
In order to facilitate the language feature at dotnet/csharplang#45, which has been approved by the LDM and mostly implemented, we should add the exception
MatchFailureException
to the framework. This exception is to be thrown when no branch of a pattern-matching switch expression matches the input at runtime.Proposal
See also dotnet/roslyn#27747
/cc @jcouv @jaredpar
The text was updated successfully, but these errors were encountered: