I'm presently trying to create the value of a key in an associative array if it does not exist, and then maintain a reference/pointer to the value. This is what I came up with, but it seems really crufty and I feel that there must be a cleaner way:

Value[string] assocArray;

foreach (...) {
  auto value = key in assocArray;
  if (!value) {
    assocArray[key] = Value();
    value = &assocArray[key];
  }
  value.memberA++;
  value.memberB = "foobar";
}


The goal was to avoid writing:
Value[string] assocArray;

foreach (...) {
  assocArray[key].memberA++;
  assocArray[key].memberB = "foobar";
}

and save a lookup call, but my solution is both less readable and also performs worse for keys not already in the associative array...

Reply via email to