Jump to page: 1 2
Thread overview
optional assignment
Dec 30
Basile B.
Dec 30
Basile B.
Dec 31
monkyyy
Dec 31
Basile B.
Jan 21
IchorDev
Jan 02
Basile B.
Jan 04
Basile B.
Feb 05
Basile B.
Feb 05
Basile B.
Feb 26
Hipreme
December 30

A common pattern in a world where null exists is

if (!a)
  a = b;

that is semantically equivalent, if a is a pointer (or if it implements opCast!bool), to 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:

a ?= b;

As a new binary assign operator.

December 30

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.

December 31

On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:

>

A common pattern in a world where null exists is

if (!a)
  a = b;

that is semantically equivalent, if a is a pointer (or if it implements opCast!bool), to 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:

a ?= b;

As a new binary assign operator.

https://forum.dlang.org/thread/nmwzohfqqyajahkncreq@forum.dlang.org

December 31

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

if (!a)
  a = b;

that is semantically equivalent, if a is a pointer (or if it implements opCast!bool), to 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:

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.

December 31

On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:

>

A common pattern in a world where null exists is

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:

a ?= b;
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);
}
January 02

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

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:

a ?= b;
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.

January 04

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

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:

a ?= b;
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.

January 21

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)

January 23

On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:

>

A common pattern in a world where null exists is

if (!a)
  a = b;

that is semantically equivalent, if a is a pointer (or if it implements opCast!bool), to 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:

a ?= b;

As a new binary assign operator.

C# has this as the ?? and ??= operator. I use it sometimes 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:

class C;

C f(int x);
C g(int x);

int n = 42;
C c = f(n) ?? g(n) ?? throw new InvalidOperationException();
February 01
```
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.
« First   ‹ Prev
1 2