[Issue 12491] [AA] Disallow non-`immutable` types as associative array keys

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jul 21 06:23:40 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=12491

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #2 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Martin Nowak from comment #1)
> How about something less drastic, e.g. AA's only allow const access to their
> keys?

That doesn't help. Const access doesn't mean nobody has mutable access.

Consider that the most common source of an error here is not that someone gets
the key from the AA, but rather that he has a mutable pointer to the key from
another source that he doesn't realize will affect the AA.

I personally think AAs simply shouldn't enforce any immutability. Enforcing
const is useless, and enforcing immutable makes keys that aren't written with
immutability in mind unusable. The result is, if you use a mutable key type for
an AA, don't change any of the keys, or the AA doesn't work any more (note that
this won't cause corruption, just leakage, so it can still be @safe).

In addition, immutability of the *entire* key isn't necessary. You could have
members that are mutable but don't affect the hash/equal calculation. It would
be impossible for the compiler to enforce this. I'd rather just leave it up to
the user.

--


More information about the Digitalmars-d-bugs mailing list