dotnet / dotnet/vblang

Un/Checked Expression, and Checked For Loop

Open
#461 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Related: #180

## Checked Expressions
C# has the the concept of check and unchecked expressions, I think we should also bring into VB.net as well. Implicitly assumed to be `Checked expression` unless explicitly stated `Unchecked`, as to retain backwards compatibility, We can do this by introducing a new keyword `Checked`, that acts like a prefix operator on expressions. eg
```vbnet
Dim value As SByte = SByte.MinValue
Dim result As Byte = unchecked( DirectCast( value, Byte) expr`
```
This is useful when you explicitly want to cast to and from signed to unsigned types, without thrown an exception.

------

## Checked For Loop
A `For-Loop` is implicitly unchecked (ie it throws exceptions) about being out of range. By prefixing the control variable with `Checked` eg `For Checked index As Sbyte = SByte.MinValue To SByte,MaxValue Step 2`. The control variable is checked to remain within the range stated, this has the side-effect of control variable after the final iteration of the loop, not having a value beyond the end value (`SByte.MaxValue` in the example). This allows the full range of the value types. eg `.MinValue To .MaxValue`

This can be achieved by altering the lowering of the for loop, in the checked case.

[Rough Lowering for Checked For Loop](https://sharplab.io/#v2:DYLgbgRgNALiBOBXAdlAJiA1AHwJIFsAHAe3hgGcACAZQE9yYBTfAWACh2BhYAQ3Ks7tKwmgAse8RmmEAFRBGABLAMY15lALI9FyABQBKISICQAQXLLGyNDoDmAfQBipAEo9ktxruoAhWkwA6DR0ANR5gREYoGj9ArQAPMIioygBmVMM2ERMAEUYLKxsPJ1d3T29YxiCeRPDI6N9/KuDkJPrKAFp0zMpjAFFrNQgjbOERyhl4RTAeJjEJKUpHFGUYRWJkSgBxRhgBtDavGhgJGEPKc0oAGQ3baP3zy5uPBqZCR6pn230Lz9vx7I5RT4Sh5YAnSgAXk0s1EAVMEHIukonCuHl0DzqjB+HRRaNs3hOZEO+n0mAAjACRECQRpiGhEFRobowRC6dJqG8SVThC5doh4JtcAAzQmnc4AHmhmOS92shw67MZcoOWMwSvIPRE+yWKzWG3G42o4kkHPU5ks1jsJXgbg8R054qxv2utxVH1dL2OjHezqety1o1GNOufDOzuhOz28qxYuJWKgMvqnJ93KyQdGnA25GIwCqAHUpkwrjovAA5Hj4RgAeVFFsK1uctrK2MoADJKAAiECQzvt0MMNMZ4PAyi4ayMWrJKHHJ3JHmjR1kWv2G7EQggBeA0fKIgz8doSeHAJZogLAAqxF0VzDQ+HIhFlF3hEoUsoAAZKOfRFYtxmAOSUD49K0JQxDCq665/kGWbIDmeYBIWihMLoAAknYAN4HkeWIAL7RJ2gb3tkj7PpQAB80Kft+v7psRw5bMQl6UH08TIauxBQXR9HCDqIrQaM2FTpElCYNCKa+vO3E8YxzFLjAK5roQAl8cKKlsTAHHrpu0kZrB8EFkWjAlsgXhEdkOrUPIC48saCxmhAoL5JaRQOE2drlLO8bTv6XpJowLpfK8qZ+n8HjmRmIY3oOEbbLs/lxuGsr+VAEl3sR+m5oZyHGaWugVlWtYss5DbFO5LY/B23a9v20VJZEEVBiGQnnOJRL1YwAnyYpnEbgJIZkdCLVYiexBnpIl7XreWKNYJEFkZRH5fj+yACdkgHAaB4GUMAvVrSImUIUhKHoVhE7CYw+FdrNw6kXub7USt+1BrJxAsRpWnKbpxGqc9Y7necYnepJkR/a9XkKcKn0qYM/HfaMrHsUpOk8Yd2XFnls2WdZ8PsDq3B8OQQA=)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.