ProtoObject and comparison for equality and ordering

H. S. Teoh hsteoh at quickfur.ath.cx
Thu May 16 14:42:00 UTC 2019


On Thu, May 16, 2019 at 08:15:07AM -0600, Jonathan M Davis via Digitalmars-d wrote:
> On Thursday, May 16, 2019 7:53:45 AM MDT Steven Schveighoffer via 
> Digitalmars-d wrote:
[...]
> > Yes, but that can be redone. Basically the TypeInfo for an object
> > uses Object.toHash expecting the base to be object (and BTW is
> > calling this on a specifically non-CONST object, even though keys
> > are supposed to be const!)
> 
> Really, they need to be immtable if you want to guarantee that they
> don't change. Having them be const doesn't really buy you anything
> other than ensuring that the AA implementation doesn't modify it
[...]

Yes, AA keys must be immutable. The current const requirement guarantees
nothing.

There is more to this, though. Lookups using non-immutable (even
mutable) keys ought to be allowed, since you only need to compare keys
in that case. But assignment must require immutable. IOW:

	// Key is any type that contains references
	Key mk;
	const Key ck;
	immutable Key ik;

	//int[Key] aa; // not allowed, require immutable
	//int[const(Key)] aa; // not allowed, require immutable
	int[immutable(Key)] aa; // OK
	// Of course, to reduce verbosity we could simply define V[Key]
	// to mean V[immutable(Key)] implicitly.

	//aa[mk] = 1;	// not allowed, require immutable
	//aa[ck] = 1;	// not allowed, require immutable
	aa[ik] = 1;	// OK

	int i;
	i = aa[mk]; // OK: lookup only
	i = aa[ck]; // OK: lookup only
	i = aa[ik]; // OK

	i = aa.get(mk, 0); // OK: lookup only
	i = aa.get(ck, 0); // OK: lookup only
	i = aa.get(ik, 0); // OK

	int* p;
	p = mk in aa;	// OK: lookup only
	p = ck in aa;	// OK: lookup only
	p = ik in aa;	// OK

	aa.remove(mk); // OK: lookup only
	aa.remove(ck); // OK: lookup only
	aa.remove(ik); // OK


T

-- 
Let's not fight disease by killing the patient. -- Sean 'Shaleh' Perry


More information about the Digitalmars-d mailing list