toHash => pure, nothrow, const, @safe

James Miller james at aatch.net
Mon Mar 12 03:01:10 PDT 2012


> That sounds intentionally.
>
> Say you have a struct with a getHash method.
>
> struct Key
> {
>    hash_t getHash() /* inferred pure */
>    {
>    }
> }
>
> Say you have an Set that requires a pure opHash.
>
> void insert(Key key) pure
> {
>    immutable hash = key.toHash();
> }
>
> Now if you change the implementation of Key.getHash
> then maybe it can no longer be inserted into that Set.
> If OTOH your set.insert were inferred pure itself, then
> the impureness would escalate to the set.insert(key) caller.
>
> It's about the same logic that would makes nothrow more useful.
> You can omit it most of the times but always have the
> possibility to enforce it, e.g. at a much higher level.

My point was more about distant code breaking. Its more to do with
unexpected behavior than code correctness in this case. As i said, I
could be worrying about nothing though.

--
James Miller


More information about the Digitalmars-d mailing list