www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23565] New: Change `$` semantics so that it works with `.ptr`

https://issues.dlang.org/show_bug.cgi?id=23565

          Issue ID: 23565
           Summary: Change `$` semantics so that it works with `.ptr` too
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: b2.temp gmx.com

We can use `.ptr` in slice and indexes expressions to bypass bounds
verification but then we lose the opportunity to use `$` for the subscripts,
even if an array and hence a `.length` is possible.

I propose to enhance the semantics of `$` so that this code compiles and
execute

```d
void main()
{
    auto b = new int[](2);
    b.ptr[0 .. $] = [1,2]; // b.ptr[0 .. b.length] = [1,2];
    assert(b == [1,2]);
    b.ptr[$-1] = 3;
    assert(b == [1,3]);
}   
```

This would break nothing as this is currently rejected, e.g for the example
with

 Error: undefined identifier `$`
--
Dec 16 2022