Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn
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,

Re: Assocative array lookup for object

2023-04-12 Thread Ali Çehreli via Digitalmars-d-learn
On 4/12/23 04:35, Salih Dincer wrote: > I made a little mistake and I'll fix it before someone rub nose in it :) You asked for it! :) >auto opIndex(string key) { > if(auto ret = key in data) > { >return *ret; > } > return null; >} Not every type is

Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 04:57:58 UTC, Salih Dincer wrote: I think you want to do an encapsulation like below. ```d auto opIndex(string key)    => *(key in data); ``` I made a little mistake and I'll fix it before someone rub nose in it :) ```d auto opIndex(string key) {

Re: Assocative array lookup for object

2023-04-11 Thread Salih Dincer via Digitalmars-d-learn
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

Re: Assocative array lookup for object

2023-04-11 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
You want the operator overload opIndex inside your class. https://dlang.org/spec/operatoroverloading.html#array

Assocative array lookup for object

2023-04-11 Thread Chris Katko via Digitalmars-d-learn
```D class bitmapHandler { bitmap*[string] bmps; void get(string name){return bmps[name]; /* plus other code */} } void usage() { bitmapHandler bh; bitmap foo = bh.get("bar"); // works bitmap foo2 = bh["bar"]; // desired } ``` Should I be using opEquals? Or something different? The problem