www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - optional assignment

reply Basile B. <b2.temp gmx.com> writes:
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
next sibling parent Basile B. <b2.temp gmx.com> writes:
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
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
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
parent Basile B. <b2.temp gmx.com> writes:
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:
 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
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.
Dec 30 2024
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
parent reply Basile B. <b2.temp gmx.com> writes:
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:
 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); } ```
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`.
Jan 02
parent Basile B. <b2.temp gmx.com> writes:
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:
 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); } ```
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`.
Also people will have to import to make it available, while `?=` would just works, out of the box.
Jan 04