Pattern matching as the mechanism to add Code Contracts (C# vNext) #3920
Replies: 6 comments 15 replies
-
This seems related to #2145, and also to my comment where a proposed syntax was
What I don't see in your syntax is how it would support the more complex expressions, null checks, etc. that code contracts do. |
Beta Was this translation helpful? Give feedback.
-
The minimal feature would be to support the pattern matching without the "when". So you would support this:
But maybe not this:
Supporting "when", while useful, would make it more likely developers would arbitrarily execute code as part of evaluating the contract. |
Beta Was this translation helpful? Give feedback.
-
I would personally prefer void SomeMethod(Foo foo)
throws ArgumentNullException(nameof(foo)) when foo is null
{
// translated directly to if (foo is null) throw new ArgumentNullException(nameof(foo));
} |
Beta Was this translation helpful? Give feedback.
-
@JustNrik I quite like that, and it does use the idea of patterns. We now, in C# 9, already have "is XXXX" and "is not XXXX" syntax with pattern matching. If we want to allow an exception to be specified, that seems like a clean enough way to do it. Note I have been using "where" instead of "when" because "when" already has a different meaning. The team use "when" to allow ad-hoc code, such as:
Thus I am using the syntax:
Because if we want to allow ad-hoc code, we then just support the when, such as:
I am not convinced "when" should be supported. I am not convinced the feature should support ad-hoc code, and suspect people smarter than me will find problems in a compiler proving these constraints in a finite time with ad-hoc code support. |
Beta Was this translation helpful? Give feedback.
-
Describing the rules is the easy part. It's what happens with those rules that is interesting and complicated. How are those rules encoded so that the compiler can statically check them at compile time? Does the compiler emit runtime checks to enforce those rules? Not everyone wants to incur the cost of checks on every invocation. If the compiler does emit runtime checks, what does it do if the rules are violated? Throw some exception? Fail fast? Contracts are fraught with policy and not everyone agrees on even the basic concerns above. That's why the existing issue hasn't moved forward, not because of syntax. |
Beta Was this translation helpful? Give feedback.
-
Code contracts have never been about preventing compilation though. They've always been about runtime correctness. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to ask if @MadsTorgersen and the C# team are considering using the new (powerful) pattern matching features as a mechanism to introduce Code Contracts to C# vNext.
Ideally the syntax would be consistent with the current pattern matching features and this would be seen as an orthogonal feature, and not something completely new as per previous attempts at integrating code contracts into the language.
Ideally we would have static analysis (or Analysers) that inform developers as they are coding if they have violated a contract.
For example, today we could write:
It would be nice to have a code contracts feature where you can pattern match using the existing mechanisms (tuple, property, etc), as shown:
If this contract is not matched an Exception should be thrown (this would be added by the compiler); and alternatively the syntax could allow you to specify the exception in code. But the key design of this feature would be for static analysis or an analyzer to provide warnings at compile time or via intellisense.
Pattern matching could be the light-weight mechanism we have been waiting for to implement code-contracts inside the language in an elegant and orthogonal way.
EDIT: I would add that my assumption, when calling a method with multiple parameters, is they would be implicitly wrapped in a tuple when performing the pattern matching on the right hand side.
Beta Was this translation helpful? Give feedback.
All reactions