digitalmars.D - One of the thing I like in D
- Boaz Ampleman (15/15) Oct 07 ```d
- Dom Disc (14/29) Oct 08 But why pointers?
- Boaz Ampleman (6/26) Oct 08 yeah sorry, that missed a bit of comments. Here there are: the
- Imperatorn (2/33) Oct 08 That's kinda flexible actually
- Walter Bright (1/1) Oct 09 Yup. It's designed to look good!
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (19/34) Oct 11 With next dmd you can use use ref variables aswell as
```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
Oct 07
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 beautifulBut why pointers? ```d void safe main() { auto a = 0; auto b = 0; auto c = 1; (c ? a : b) = 8; assert(a == 8); (c ? b : a) = 2; assert(a + b == 10); } ```
Oct 08
On Tuesday, 8 October 2024 at 08:38:15 UTC, Dom Disc wrote:On Monday, 7 October 2024 at 20:03:46 UTC, Boaz Ampleman wrote:yeah sorry, that missed a bit of comments. Here there are: the point was that the conditional expression yields a lvalue, hence it's candidate to be a sub-expression of the address expression. That is just something I had never seen before. I just wanted to share the joy ;)```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 beautifulBut why pointers? [...]
Oct 08
On Tuesday, 8 October 2024 at 08:38:15 UTC, Dom Disc wrote:On Monday, 7 October 2024 at 20:03:46 UTC, Boaz Ampleman wrote:That's kinda flexible actually```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 beautifulBut why pointers? ```d void safe main() { auto a = 0; auto b = 0; auto c = 1; (c ? a : b) = 8; assert(a == 8); (c ? b : a) = 2; assert(a + b == 10); } ```
Oct 08
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 beautifulWith 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.
Oct 11
On Friday, 11 October 2024 at 23:04:20 UTC, Per Nordlöw wrote: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.This has to be a bug ;)
Oct 11
On Friday, 11 October 2024 at 23:04:20 UTC, Per Nordlöw wrote:On Monday, 7 October 2024 at 20:03:46 UTC, Boaz Ampleman wrote:`ref` can’t change what it is pointing to. So the second ternary is just assigning through the reference, not re-binding the reference. If you re-write the example in terms of pointers, it is equivalent to: ```d void main() { auto a = 0; auto b = 0; const c = 1; int* ptr = &(c ? a : b); *ptr = 8; assert(a == 8); *ptr = (c ? b : a); *ptr = 2; } ```[...] It's just beautifulWith 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.
Oct 11