Assocative array lookup for object

Salih Dincer salihdb at hotmail.com
Wed Apr 12 04:57:58 UTC 2023


On Wednesday, 12 April 2023 at 01:16:17 UTC, Chris Katko wrote:
> Should I be using opEquals? Or something different? The problem 
> with 'alias this' here is I want to wrap access to the insides 
> with getter functions that do various things like logging and 
> error checking.

I think you want to do an encapsulation like below.

```d
class Handler(T)
{
   T*[string] data;

   auto set(string key, ref T value)
     => data[key] = &value;

   auto opIndex(string key)
     => *(key in data);
}

void main()
{
   class Bitmap {}
   Bitmap foo;

   auto handler = new Handler!Bitmap;
   handler.set("D Lang", foo);

   assert(handler["D Lang"] == &foo);
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list