www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - No compile time bounds checking for static arrays?

reply Tejas <notrealemail gmail.com> writes:
```d
import std;
void main()
{
     int[40] staticA;
     auto c = staticA[0..50];//mistake
}```
results in:

```d
core.exception.RangeError onlineapp.d(5): Range violation
----------------
??:? _d_arrayboundsp [0x55db29a0b645]
./onlineapp.d:5 _Dmain [0x55db29a0ae8c]
```

Is there a way to make this check compile time?
Aug 10 2021
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Wednesday, 11 August 2021 at 06:29:40 UTC, Tejas wrote:
 ```d
 import std;
 void main()
 {
     int[40] staticA;
     auto c = staticA[0..50];//mistake
 }```
 results in:

 ```d
 core.exception.RangeError onlineapp.d(5): Range violation
 ----------------
 ??:? _d_arrayboundsp [0x55db29a0b645]
 ./onlineapp.d:5 _Dmain [0x55db29a0ae8c]
 ```

 Is there a way to make this check compile time?
Without modifying the compiler? No. Please file an enhancement request on issues.dlang.org. This particular example is an easy fix.
Aug 10 2021
parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 11 August 2021 at 06:46:34 UTC, Stefan Koch wrote:
 On Wednesday, 11 August 2021 at 06:29:40 UTC, Tejas wrote:
 ```d
 import std;
 void main()
 {
     int[40] staticA;
     auto c = staticA[0..50];//mistake
 }```
 results in:

 ```d
 core.exception.RangeError onlineapp.d(5): Range violation
 ----------------
 ??:? _d_arrayboundsp [0x55db29a0b645]
 ./onlineapp.d:5 _Dmain [0x55db29a0ae8c]
 ```

 Is there a way to make this check compile time?
Without modifying the compiler? No. Please file an enhancement request on issues.dlang.org. This particular example is an easy fix.
[Done](https://issues.dlang.org/show_bug.cgi?id=22198)
Aug 11 2021