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
- IchorDev (3/9) Jan 21 By the way ‘the shit’ means ‘the best’.
- Paul Backus (21/32) Dec 31 2024 ```d
- Basile B. (6/41) Jan 02 Paul while your template is somewhat equivalent, I have doubts
- Basile B. (3/49) Jan 04 Also people will have to import to make it available, while `?=`
- Quirin Schroll (16/33) Jan 22 C# has this as the `??` and `??=` operator. I use it sometimes
- Walter Bright (5/5) Feb 01 ```
- Quirin Schroll (31/36) Feb 05 Compare:
- Quirin Schroll (3/22) Feb 05 This is the actually equivalent code if we’re pedantic.
- Basile B. (31/36) Feb 05 I dont't know what regex have you used but here I've just tried
- Basile B. (2/12) Feb 05 forgot the rparen: `if( )+\(\![a-zA-Z_][a-zA-Z_0-9.]*\)`
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 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
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
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
``` 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
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
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:This is the actually equivalent code if we’re pedantic.``` 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; } ```
Feb 05
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
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:forgot the rparen: `if( )+\(\![a-zA-Z_][a-zA-Z_0-9.]*\)```` 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:
Feb 05