digitalmars.dip.ideas - optional assignment
- Basile B. (17/17) Dec 30 2024 A common pattern in a world where `null` exists is
- Basile B. (3/6) Dec 30 2024 By te way `?=` would be a new lexical token, not `?` then `=`.
- monkyyy (2/19) Dec 30 2024 https://forum.dlang.org/thread/nmwzohfqqyajahkncreq@forum.dlang.org
- Basile B. (6/31) Dec 30 2024 I use it since monthes in styx. It's one of the best thing that's
- Paul Backus (21/32) Dec 31 2024 ```d
A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` that is semantically equivalent, if `a` is a pointer (or if it implements opCast!bool), to D ```d if (a is null) a = b; ``` I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``` As a new binary assign operator.
Dec 30 2024
On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:A common pattern in a world where `null` exists is [...] As a new binary assign operator.By te way `?=` would be a new lexical token, not `?` then `=`. Really the both.
Dec 30 2024
On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` that is semantically equivalent, if `a` is a pointer (or if it implements opCast!bool), to D ```d if (a is null) a = b; ``` I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``` As a new binary assign operator.https://forum.dlang.org/thread/nmwzohfqqyajahkncreq forum.dlang.org
Dec 30 2024
On Tuesday, 31 December 2024 at 00:38:56 UTC, monkyyy wrote:On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:I use it since monthes in styx. It's one of the best thing that's possible just right now in D. Otherwise I would not propose it. For a long time I thought optional access was really the shit, but at some point I implemented a programming language, and now I'm pretty sure that is the best niche expression.A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` that is semantically equivalent, if `a` is a pointer (or if it implements opCast!bool), to D ```d if (a is null) a = b; ``` I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``` As a new binary assign operator.https://forum.dlang.org/thread/nmwzohfqqyajahkncreq forum.dlang.org
Dec 30 2024
On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` [...] I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``````d ref optAssign(T, U)(ref T dest, U value) { if (!dest) dest = value; return dest; } unittest { int n; int* p; n.optAssign(123); assert(n == 123); n.optAssign(456); assert(n == 123); p.optAssign(&n); assert(*p == 123); p.optAssign(new int(456)); assert(*p == 123); } ```
Dec 31 2024
On Tuesday, 31 December 2024 at 18:10:52 UTC, Paul Backus wrote:On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:Paul while your template is somewhat equivalent, I have doubts over people using it ;) The first problem I see is that it will be inline-ed only with certains command line arguments, while the proposed expression does not involved a call, or `-O`, or `-inline`.A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` [...] I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``````d ref optAssign(T, U)(ref T dest, U value) { if (!dest) dest = value; return dest; } unittest { int n; int* p; n.optAssign(123); assert(n == 123); n.optAssign(456); assert(n == 123); p.optAssign(&n); assert(*p == 123); p.optAssign(new int(456)); assert(*p == 123); } ```
Jan 02
On Thursday, 2 January 2025 at 16:52:01 UTC, Basile B. wrote:On Tuesday, 31 December 2024 at 18:10:52 UTC, Paul Backus wrote:Also people will have to import to make it available, while `?=` would just works, out of the box.On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:Paul while your template is somewhat equivalent, I have doubts over people using it ;) The first problem I see is that it will be inline-ed only with certains command line arguments, while the proposed expression does not involved a call, or `-O`, or `-inline`.A common pattern in a world where `null` exists is ```d if (!a) a = b; ``` [...] I propose the get rid of the statement layer. The two statments (there are more actually) can be a single expression: ```d a ?= b; ``````d ref optAssign(T, U)(ref T dest, U value) { if (!dest) dest = value; return dest; } unittest { int n; int* p; n.optAssign(123); assert(n == 123); n.optAssign(456); assert(n == 123); p.optAssign(&n); assert(*p == 123); p.optAssign(new int(456)); assert(*p == 123); } ```
Jan 04