Implicit conversions for AA keys

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Mar 24 09:14:18 PDT 2012


On Sat, Mar 24, 2012 at 12:34:56PM +0100, Timon Gehr wrote:
[...]
> This solution creates unnecessary template bloat for every implicit
> conversion, duplicates compiler logic, and I think it does not work
> correctly because of other issues. I have refined my proof of
> concept.
[...]

OK, I've implemented your changes in a branch, but found the following
problems:

- Doesn't work with const AA/array keys, e.g.
    AA!(string[const int[]]) aa;
    AA!(string[const AA!(int[int])]) meta;
  (The AA!() template is basically just syntactic sugar that uses
  reflection to map "real" AA types into AssociativeArray templates,
  courtesy of Andrej Mitovic.)

- Doesn't work with static array keys with dynamic array lookups.

But I think I like your approach better. Creating a new template
instantiation for every implicit conversion leads to lots of code bloat,
and also breaks in many places due to bugs in IFTI. Using non-template
methods takes advantage of the compiler's implicit conversions, and
where necessary we can just add overloads for stuff like .idup, and
probably slicing for the static array key case.

One concern, though: wouldn't cast(immutable) break immutable
guarantees? For example:

	class C {
		int x;
		hash_t toHash() { ... }
	}

	AA!(int[C]) aa;
	auto c = new C;
	c.x = 123;
	const d = c;	// convert mutable C to const(C)
	aa[d] = 123;	// const(C) casted into immutable(C)
	c.x = 321;	// uh-oh: immutability broken


T

-- 
I'm still trying to find a pun for "punishment"...


More information about the Digitalmars-d mailing list