On Tuesday, 4 September 2018 at 12:27:47 UTC, nkm1 wrote:
I also had this problem recently. I think aa.require() should
allow to add immutables (feature request). Anyway, my
workaround was along the lines of:
final class AA(Key, Value)
{
Value[] _storage;
size_t[Key] _aa;
void opIndexAssign(Value value, Key key)
{
if (key !in _aa)
{
_storage ~= value;
_aa[key] = _storage.length - 1;
}
}
Value opIndex(Key key)
{
if (auto index = key in _aa)
return _storage[*index];
throw new Exception("no key");
}
}
immutable struct S
{
int num;
}
void main()
{
import std.stdio : writeln;
auto aa = new AA!(string, S);
aa["one"] = S(1);
aa["two"] = S(2);
writeln(aa["one"]);
writeln(aa["two"]);
}
Thanks for the replies. It seems quite annoying.
I just use classes for now.