On Saturday, 31 August 2024 at 14:25:29 UTC, Paul Backus wrote:
[...]
Once the next release of Phobos comes out, with [PR 9039][1] merged, you'll be able to do it like this:

```d
import std.typecons;

Nullable!V maybeGet(K, V)(V[K] aa, K key)
{
    if (auto ptr = key in aa)
        return nullable(*ptr);
    else
        return Nullable!V.init;
}

void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto it = test.maybeGet("hello"))
    {
        writeln("hello => ", it.get);
    }
}
```

Is that functionally different from

```
void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto p = "hello" in test)
    {
        writeln("hello => ", *p);
    }
}
```

?
  • Re: Associative ... Paul Backus via Digitalmars-d-learn
    • Re: Associa... ryuukk_ via Digitalmars-d-learn
      • Re: Ass... ryuukk_ via Digitalmars-d-learn
        • Re:... Lance Bachmeier via Digitalmars-d-learn
          • ... ryuukk_ via Digitalmars-d-learn
          • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
        • Re:... Nick Treleaven via Digitalmars-d-learn
          • ... Nick Treleaven via Digitalmars-d-learn
          • ... Nick Treleaven via Digitalmars-d-learn
    • Re: Associa... kdevel via Digitalmars-d-learn
      • Re: Ass... Paul Backus via Digitalmars-d-learn
        • Re:... ryuukk_ via Digitalmars-d-learn
          • ... evilrat via Digitalmars-d-learn

Reply via email to