So how do you create an associative array with an immutable value, anyways?

Steven Schveighoffer schveiguy at gmail.com
Fri Oct 7 13:58:52 UTC 2022


On 10/7/22 2:58 AM, FeepingCreature wrote:
> `assocArray` famously complains that it needs a mutable value type. You 
> can't set values. You could write a literal, but that only works for a 
> finite number of keys, and there's no way to merge two associative arrays.
> 
> Internally, we use librebindable, so we can create a 
> `Rebindable!(immutable V)[K]`, which is mutable, and cast it to 
> `immutable V[K]` without breaking anything. But I'm not sure how I'd 
> propose getting that into Phobos without including librebindable 
> outright. But I also don't see another way to do it?
> 
> Any ideas?

`require` should work, since it effectively only assigns a value if it 
doesn't already exist.

If it doesn't, then maybe it can be fixed so it does.

We can't allow just general assignment to compile, as you may have an 
already existing value.

-Steve


More information about the Digitalmars-d mailing list