On Wednesday, 12 April 2023 at 13:09:07 UTC, Ali Çehreli wrote:
Not every type is null'able but nullable. ;) So, a design may use the following:
https://dlang.org/library/std/typecons/nullable.html

I implemented Handler into the Voldermort build, which Walter loved so much. For convenience, I put an alias between the template and the function. But Nullable didn't work, instead it's returning T.init...

```d
template Handler(alias A)
{
  alias T = typeof(A);

  auto Handler()
  {
    struct Impl
    {
      T*[string] data;

      void set(string key, ref T value)
      {
        data[key] = &value;
      }

      auto opIndex(string key)
      {
        if (auto ret = key in data)
        {
          return **ret;
        }
        return T.init;/*
        import std.typecons : Nullable;
        return Nullable!T.init;//*/
      }

      auto opSlice()
      {
        T[] result;
        foreach (ref value; data.values)
          result ~= *value;
        return result;
      }
    }
    return Impl();
  }
}

import std.stdio;

void main()
{
  struct List { string product; float price; }

  auto fruits = [ List("Manderin", 3.79),
                  List("Orange", 2.99),
                  List("Kiwi", 0.59),
  ];

  auto handlers = Handler!fruits;
  handlers.set("fruits", fruits);
  // please try it:  ^------v
  foreach(h; handlers["fruit"])
  {
    h.product.write(": ");
    h.price.writeln(" €");
  }

  auto handler = Handler!(List());

  import std.conv : text;
  foreach(i, ref fruit; fruits)
  {
    handler.set(i.text, fruit);
  }
  handler[].writeln;
} /* Prints:

  Manderin: 3.79 €
  Orange: 2.99 €
  Kiwi: 0.59 €
[List("Kiwi", 0.59), List("Manderin", 3.79), List("Orange", 2.99)]

*/
```

SDB@79

  • Assocative array... Chris Katko via Digitalmars-d-learn
    • Re: Assocat... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: Assocat... Salih Dincer via Digitalmars-d-learn
      • Re: Ass... Salih Dincer via Digitalmars-d-learn
        • Re:... Ali Çehreli via Digitalmars-d-learn
          • ... Salih Dincer via Digitalmars-d-learn

Reply via email to