Populating nested AAs; "This wouldn't be so verbose in Ada 2020"

mipri mipri at minimaltype.com
Sun Dec 8 04:17:45 UTC 2019


Hello,

I've got this code:

   struct UserStats {
     int ok, evil;
   }

   // module-level variable
   UserStats[string][string] userHistory;

and this code that populates it:

   // loop over DB query

   if (host !in userHistory)
       userHistory[host] = typeof(userHistory[host]).init;

   if (user !in userHistory[host])
       userHistory[host][user] = 
typeof(userHistory[host][user]).init;

   if (ok) ++userHistory[host][user].ok;
   else ++userHistory[host][user].evil;

The joke of the thread title is that in pseudo-Ada2020, those
assignments would look like

   User_History (host) = typeof (@).init;
   User_History (host) (user) = typeof (@).init;

as @ is a shortcut for the left hand side of an assignment.
It's what Ada has instead of += and friends.

Of course, I don't propose that @ be added to D; I just want to
know if there's a good idiom for new assignments to nested AAs
like this. A nice alternative for my specific use is

   struct UserStats {
     static int[Tuple!(string, string)] ok, evil;
   }

and

   // loop over DB query
   if (ok) ++UserStats.ok[tuple(host, user)];
   else ++UserStats.evil[tuple(host, user)];

But of course this would be less convenient if f.e. I wanted to
pattern my data structures after a JSON production result.



More information about the Digitalmars-d-learn mailing list