The design suggestion assert keyword: have compiler emit call to Debug.Assert overload with CallerArgumentExpression when possible has been marked "approved in principle".
This RFC covers the detailed proposal for this suggestion.
- Suggestion
- Approved in principle
- Implementation
- Discussion
When an assert expression failed, it will shows the text of the expression in the exception message.
When the assert expression failed, it does not provide the details of the expression, but the System.Diagnostics.Debug.Assert does.
assert <bool expression> will be translated into System.Diagnostics.Debug.Assert(<bool expression>, "<bool expression>").
assert (1 + 1 = 2) // This will be translated into
System.Diagnostics.Debug.Assert((1 + 1 = 2), "(1 + 1 = 2)")Since the Debug.Assert(Boolean, String) overload has the same supporting runtime as the Debug.Assert(Boolean), changing the assert expression to the former overload will not cause runtime errors.
In 6.5.12 Assertion Expressions, System.Diagnostics.Debug.Assert(expr) changes to System.Diagnostics.Debug.Assert(<expr>, "<expr>").
No.
By supporting [<OverloadResolutionPriorityAttribute>] introduced in .NET 9, the compiler can auto take the overload with CallerArgumentExpression when possible, and will not need this particular RFC. See the comment from the original suggestion.
The drawback of this alternative is that it will not work with .NET 8 and below.
With supporting [<CallerArgumentExpressionAttribute>], the System.Diagnostics.Debug.Assert(<bool expression>) can show the text of the expression in the exception message. This RFC is making assert expression works the same way.
Please address all necessary compatibility questions:
- Is this a breaking change?
If there are user-overridden
System.Diagnostics.Debug.Asserts, and there is no overload with signature(condition: bool * message: string) -> ..., this will be a breaking change. - What happens when previous versions of the F# compiler encounter this design addition as source code?
It will works the same as before.
- What happens when previous versions of the F# compiler encounter this design addition in compiled binaries?
It will works the same as before.
- If this is a change or extension to FSharp.Core, what happens when previous versions of the F# compiler encounter this construct?
N/A
- What happens when this feature is consumed by another .NET language?
It will works the same as before.
- Are there any planned or proposed features for another .NET language (e.g., C#) that we would want this feature to interoperate with?
N/A
There can be a friendly error message when the System.Diagnostics.Debug.Assert: (condition: bool * message: string) -> ... overload is absent.
This feature may impact the compilation speed when the code file is too large and has many assert expressions, since it needs get substrings from the file.
- Expected maximum number of widgets in reasonable hand-written code: 1/3 of code lines
- Expected reasonable upper bound for number of widgets accepted: 1/2 of code lines
Testing should particularly check that compilation is linear (or log-linear or similar) along these dimensions. If quadratic or worse this should ideally be noted in the RFC.