February 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.

Compare:

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 C# for lazy init containers. It’s a common pattern.

An if statement forces a body, forces braces with many linters, and (technically) requires evaluating the operand twice so it’s not even equivalent.

int getField3()
{
    auto ref field = _field;
    if (!field)
    {
        field = new int(10);
    }
    return *field;
}

This is the actually equivalent code if

February 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:

>
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.

[…]

int getField3()
{
    auto ref field = _field;
    if (!field)
    {
        field = new int(10);
    }
    return *field;
}

This is the actually equivalent code if we’re pedantic.

February 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:

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

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".

February 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:

>
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.]*\)

February 25

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

February 25

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

February 26

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.

Currently, I've been using std for that.

import std.algorithm.comparison;
import std.stdio;

void main()
{
    int a;
    a = either(a, 500);
    writeln = a;
}
1 2
Next ›   Last »