[Proposal]: VB New Feature Unchecked math
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
This is to open a "Feature Specification" following the process "roslyn/docs/contributing/Developing a Language Feature.md" for PR VB Prototype "Checked expressions" dotnet/roslyn#41104 done by @AdamSpeight2008 and closed by @jaredpar for not following the process above. This is not to endorse or comment on that implementation but to start again and follow the process.
* A description of the feature (including any syntax changes involved)
A new Keyword is added to Visual Basic **Unchecked**
The Unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions during compilation and run-time.
In an unchecked context, if an expression produces a value that is outside the range of the destination type, the overflow is not flagged. For example, because the calculation in the first example line is performed in an unchecked context, the fact that the result is too large for an integer is ignored, and int1 is assigned the value Integer.MinValue.
If the Unchecked Keyword is removed line 2 below, a compilation error occurs. The overflow can be detected at compile time because all the terms of the expression are constants.
```VB
Dim int1 as Integer = Unchecked (Integer.Max + 1) ' No compiler error
Dim int2 as Integer = Integer.Max + 1 ' Compiler error
```
This feature also provides an Unchecked Override for all mathematical operations that could cause Overflow done inside the function. so they don't throw math exceptions.
```VB
Private MaxUInteger As Uinteger= UInteger.MaxValue
' Stuff
Assert.Equal(UInteger.MinValue, Unchecked (MaxUInteger + CUInt(1)))
Assert.Throws(Of OverflowException)(Function() As UInteger
Return MaxUInteger + CUInt(1)
End Function)
```
* Discussions about impacted areas, such as overload resolution and type inference. Think through the major areas of the language specification while determine the potential impacts
Since the overload only happens within the Unchecked function it should not effect overload resolution and type inference. One issue is the new Keyword Unchecked if someone were using Unchecked as a variable name. This was the same issue for NameOf and should be handled the same way.
* Proposed changes to the API surface area.
I don't think this applies but the existing BoundExpression already has support for checked / unchecked expressions. There is an additional use of the Unchecked Keyword in For loops that should be looked into at the same time.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.