Checked and Unchecked code blocks

Topics: VB Language Design
Apr 16, 2014 at 9:34 AM
Checked and Unchecked code blocks are missing from VB but in C# they are already there.
It is only possible to enable or disable overflow checking for the whole project and not for code blocks.
This complicates things, for example implementing GetHashCode().
Apr 18, 2014 at 12:18 PM
Edited Apr 18, 2014 at 12:39 PM
Sorry for just bumping.
I see that the others got answered, except this, maybe not intentionally.
May 1, 2014 at 7:33 PM
An alternative I think would be better would be explicit "wrapping" integer types. Declare a variable as Wrapping Integer, Wrapping Long, Wrapping Byte, etc. and expressions involving the variable would behave as unchecked, without affecting operations on other values.

Operations between a wrapping type and non-wrapping type should yield the same wrapping type, regardless of the size of the other operand. Operations between two wrapping types should work as expected if they are the same size, and be forbidden if they are not. Wrapping types should not implicitly convert to normal integers, but should behave as though they they have members AsSigned and AsUnsigned which yield like-sized integer equivalents.
Developer
May 2, 2014 at 12:14 AM
If time permits we may get this in.
May 2, 2014 at 8:47 PM
nmgafter wrote:
If time permits we may get this in.
I what form? Do you like the idea of separate wrapping types? I forgot to mention that if they are included, it might be good to allow easy declaration of wrapping-integer literals (perhaps by including a "W").
Developer
May 5, 2014 at 11:30 PM
If we do it for this upcoming release we'd almost certainly copy the design from C# rather than going in a completely separate direction. We don't have time to design and implement new wrapper types.
May 6, 2014 at 6:52 AM
In the upcoming release there could be C# like checked and unchecked blocks in VB.NET, and in the next release there could be wrapping types for both VB.NET and C#. Then if a variable is checked it is dependent upon compiler settings/is it in a checked block/is it a wrapping type. If it's a wrapping type then it is the most high priority rule even if it's in an unchecked block.
May 6, 2014 at 1:37 PM
nmgafter wrote:
If we do it for this upcoming release we'd almost certainly copy the design from C# rather than going in a completely separate direction. We don't have time to design and implement new wrapper types.
One objection I have with checked/unchecked blocks as I understand their implementation is that there's no way to distinguish what I would consider the three relevant states:
  • I want overflow checking here, even if it costs performance.
  • I expect this to overflow, and need computations to wrap mod 2^n.
  • This shouldn't overflow. and I wouldn't mind a trap if it did, but in high-performance builds the check may be skipped.
Would you see any difficulty having the VB.NET version of checked/unchecked blocks include all three states? I would posit that the vast majority of code should use the first mode, and things like hash code computations should use the second. High-performance code which isn't expecting things to wrap should use the third, however, rather than the second. That would both allow overflow checking to globally be enabled in such code if necessary to troubleshoot problems caused by overflow, and would also allow for the possibility that a future processor might include a zero-overhead overflow trapping feature [e.g. include instructions for "signed add with overflow trap" and "unsigned add with overflow trap"]