One of the thing I like in D

Per Nordlöw per.nordlow at gmail.com
Fri Oct 11 23:04:20 UTC 2024


On Monday, 7 October 2024 at 20:03:46 UTC, Boaz Ampleman wrote:
> ```d
> void main()
> {
>     auto a = 0;
>     auto b = 0;
>     auto c = 1;
>
>     auto ptr = &(c ? a : b);
>     *ptr = 8;
>     assert(a == 8);
>     ptr = &(c ? b : a);
>     *ptr = 2;
>     assert(a + b == 10);
> }
> ```
>
> It's just beautiful

With next dmd you can use use ref variables aswell as

```d
void main() {
     auto a = 0;
     auto b = 0;
     const c = 1;

     ref ptr = (c ? a : b);
     ptr = 8;
     assert(a == 8);
     ptr = (c ? b : a);
     ptr = 2;
     assert(b == 2); 			// why does this fail??
}
```

But for some reason the last statement doesn't behave as I 
believe it should. Is this behavior expected?

For reference see 
https://dlang.org/changelog/pending.html#dmd.reflocal.


More information about the Digitalmars-d mailing list