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 reply 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
parent reply jakasspeech50 <j50 sensualspeech.com> writes:
On Monday, 30 December 2024 at 15:13:24 UTC, Basile B. wrote:
 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.
Correct, ?= should be treated as a new token, not two separate ones
Feb 25 2025
parent jakasspeech50 <j50 sensualspeech.com> writes:
On Tuesday, 25 February 2025 at 11:27:06 UTC, jakasspeech50 wrote:
 On Monday, 30 December 2024 at 15:13:24 UTC, Basile B. wrote:
 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. 
 [URL=https://eldfall-chronicles.com/product-category/miniatures/]here[/URL]
By te way `?=` would be a new lexical token, not `?` then `=`. Really the both. [URL=https://www.viberate.com/export-music-datasets/]export music datasets[/URL]
Correct, ?= should be treated as a new token, not two separate ones
cool cool
Feb 25 2025
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 reply 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
parent IchorDev <zxinsworld gmail.com> writes:
On Tuesday, 31 December 2024 at 02:12:59 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.
By the way ‘the shit’ means ‘the best’. (https://en.wiktionary.org/wiki/the_shit)
Jan 21 2025
prev sibling next 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 2025
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 2025
prev sibling next sibling parent Quirin Schroll <qs.il.paperinik 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.
and it’s handy. I don’t care much about its exact syntax (some will suggest `?:` for the binary one, but `??=` looks a lot better than `?:=` IMO). It should be overloadable, too: `opBinary!"??"` and `opBinaryRight!"??"` and `opOpAssign!"??"` should worl as intended. It’s also an easy way to chicken out of null sutff: ```d class C; C f(int x); C g(int x); int n = 42; C c = f(n) ?? g(n) ?? throw new InvalidOperationException(); ```
Jan 22 2025
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
```
if (!a) a = b;
```

Doesn't seem that bad. I did some searches and found hardly any instances of
it, 
though that was across the dmd sources.
Feb 01 2025
next sibling parent reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Sunday, 2 February 2025 at 03:30:17 UTC, Walter Bright wrote:
 ```
 if (!a) a = b;
 ```

 Doesn't seem that bad. I did some searches and found hardly any 
 instances of it, though that was across the dmd sources.
Compare: ```d int* _field = null; // lazy init int getField1() => *(_field ??= new int(10)); int getField2() { if (!_field) { _field = new int(10); } return *_field; } ``` I see `get => list ??= new List();` and the like all the time in An `if` statement forces a body, forces braces with many linters, and (technically) requires evaluating the operand twice so it’s not even equivalent. ```d int getField3() { auto ref field = _field; if (!field) { field = new int(10); } return *field; } ``` This is the actually equivalent code if
Feb 05 2025
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Wednesday, 5 February 2025 at 12:20:33 UTC, Quirin Schroll 
wrote:
 On Sunday, 2 February 2025 at 03:30:17 UTC, Walter Bright wrote:
 ```
 if (!a) a = b;
 ```

 Doesn't seem that bad. I did some searches and found hardly 
 any instances of it, though that was across the dmd sources.
[…] ```d int getField3() { auto ref field = _field; if (!field) { field = new int(10); } return *field; } ```
This is the actually equivalent code if we’re pedantic.
Feb 05 2025
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Sunday, 2 February 2025 at 03:30:17 UTC, Walter Bright wrote:
 ```
 if (!a) a = b;
 ```

 Doesn't seem that bad. I did some searches and found hardly any 
 instances of it, though that was across the dmd sources.
I dont't know what regex have you used but here I've just tried `if( )+\(\![a-zA-Z_][a-zA-Z_0-9.]*\` and almost directly found an interesting example in typesem.d: ```d Type referenceTo(Type type) { if (type.ty == Terror) return type; if (!type.rto) { Type t = new TypeReference(type); type.rto = t.merge(); } return type.rto; } ``` could become ```d Type referenceTo(Type type) { if (type.ty == Terror) return type; return type.rto ?= new TypeReference(type).merge(); } ``` in particular here the opt-assign saves the double load on `type` to obtain `type.rto` as a lvalue. More generally to the criticism that is "that does not seem very useful" I'd reply that at some point new language additions are necessarily "niches".
Feb 05 2025
parent Basile B. <b2.temp gmx.com> writes:
On Wednesday, 5 February 2025 at 15:34:29 UTC, Basile B. wrote:
 On Sunday, 2 February 2025 at 03:30:17 UTC, Walter Bright wrote:
 ```
 if (!a) a = b;
 ```

 Doesn't seem that bad. I did some searches and found hardly 
 any instances of it, though that was across the dmd sources.
I dont't know what regex have you used but here I've just tried `if( )+\(\![a-zA-Z_][a-zA-Z_0-9.]*\` and almost directly found an interesting example in typesem.d:
forgot the rparen: `if( )+\(\![a-zA-Z_][a-zA-Z_0-9.]*\)`
Feb 05 2025
prev sibling parent Hipreme <msnmancini hotmail.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.
Currently, I've been using std for that. ```d import std.algorithm.comparison; import std.stdio; void main() { int a; a = either(a, 500); writeln = a; } ```
Feb 26 2025