6 days ago

On Tuesday, 1 April 2025 at 08:49:17 UTC, Dejan Lekic wrote:

>

On Friday, 28 March 2025 at 19:47:11 UTC, Paul Backus wrote:

>

Personally I would much, much rather have custom "operators" written using letters and words, which I can easily read and search for, than with symbols and punctuation marks.

100%

I dream of having something like this: https://kotlinlang.org/docs/functions.html#infix-notation

I wonder would that be too much for D, it already has too many features...

That particular thing should be an operator.

The general function-as-an-infix operator thing possibly complicates the parser a lot. It’s a completely different idea that definitely requires a DIP.

6 days ago

On Friday, 28 March 2025 at 17:06:56 UTC, Paul Backus wrote:

>

On Thursday, 27 March 2025 at 00:26:27 UTC, Andrey Zherikov wrote:

>

See wiki for details.

Proposed syntax:

A ?: B

This is equivalent to:

auto R = A;  // temporary (hidden) variable
(cast(bool) R) ? R : B;

Semantics: return A if it's truthy, or B otherwise.

Benefit: avoids evaluating if A twice which might be a call to some function with side effects.

Library version:

auto orElse(T)(T a, lazy T b)
{
    return a ? a : b;
}

unittest
{
    string s1 = "hello";
    string s2 = null;

    assert(s1.orElse("goodbye") == "hello");
    assert(s2.orElse("goodbye") == "goodbye");
}

That orElse cannot preserve value categories. Adding auto ref to ´lazy T b` isn’t something the language supports. I’m not sure why, maybe it’s merely an oversight, maybe it’s because it’s complicated.

auto ref orElse(T, DG)(auto ref T a, DG b)
{
    return a ? a : b();
}

void test()
{
    int x;
    int y;
    x.orElse(auto ref () => y)  = 10;
    assert(y == 10);

    x = 1;
    x.orElse(auto ref () => y)  = 11;
    assert(x == 11 && y == 10);
}

It works, but it’s not really great.

1 2
Next ›   Last »