Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
Currently my AA implementation supports automatic key conversion (as suggested by Andrei), for example: AA!(string,int) aa; char[] key = "abc".dup; aa[key] = 1;// automatically converts char[] to // string via .idup The way this

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
Why do you not just do the conversion and then compute the hash, even if the representation is the same?

Re: Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 07:01:46PM +0100, Timon Gehr wrote: > Why do you not just do the conversion and then compute the hash, even > if the representation is the same? Because Andrei says that the .idup shouldn't be performed unless it's necessary (e.g., you should be able to lookup char[] in a s

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
On 03/23/2012 07:10 PM, H. S. Teoh wrote: On Fri, Mar 23, 2012 at 07:01:46PM +0100, Timon Gehr wrote: Why do you not just do the conversion and then compute the hash, even if the representation is the same? Because Andrei says that the .idup shouldn't be performed unless it's necessary (e.g.,

Re: Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 07:18:05PM +0100, Timon Gehr wrote: > On 03/23/2012 07:10 PM, H. S. Teoh wrote: > >On Fri, Mar 23, 2012 at 07:01:46PM +0100, Timon Gehr wrote: > >>Why do you not just do the conversion and then compute the hash, > >>even if the representation is the same? > > > >Because Andr

Re: Implicit conversions for AA keys

2012-03-23 Thread Andrei Alexandrescu
On 3/23/12 12:54 PM, H. S. Teoh wrote: Currently my AA implementation supports automatic key conversion (as suggested by Andrei), for example: AA!(string,int) aa; char[] key = "abc".dup; aa[key] = 1;// automatically converts char[] to

Re: Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 01:31:28PM -0500, Andrei Alexandrescu wrote: [...] > Let's see what requirements need to be satisfied by []. Say k is a > value of the key type and x is a value being looked up. > > First, we need to be able to evaluate k == x. So the types must be > comparable. > > Second

Re: Implicit conversions for AA keys

2012-03-23 Thread Andrei Alexandrescu
On 3/23/12 1:48 PM, H. S. Teoh wrote: How do we check at compile time whether the hash function resolves to the same entity? int fun(T)(T x) { return 42; } void main() { static assert(&fun!int != &fun!double); } This actually reveals a compiler bug: Assertion failed: (d->purity != PU

Re: Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 02:06:15PM -0500, Andrei Alexandrescu wrote: [...] > A cast would be needed anyway because they have different types, > too. Anyway upon more thinking maybe this is too restrictive a rule. > It won't catch e.g. functions that are, in fact, identical, but come > from distinct

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
On 03/23/2012 08:06 PM, Andrei Alexandrescu wrote: Casting is very different from to, and useless for your purposes. You must use to. Andrei druntime mustn't depend on Phobos, and I don't see why it is necessary. What kind of functionality do you want to provide that depends on std.conv.to

Re: Implicit conversions for AA keys

2012-03-23 Thread Andrei Alexandrescu
On 3/23/12 2:30 PM, H. S. Teoh wrote: Wouldn't that require moving std.conv into druntime? And std.conv does depend on std.traits as well... Not sure how it's best to address this. Andrei

Re: Implicit conversions for AA keys

2012-03-23 Thread Andrei Alexandrescu
On 3/23/12 2:28 PM, Timon Gehr wrote: On 03/23/2012 08:06 PM, Andrei Alexandrescu wrote: Casting is very different from to, and useless for your purposes. You must use to. Andrei druntime mustn't depend on Phobos, and I don't see why it is necessary. What kind of functionality do you want t

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
On 03/23/2012 09:05 PM, Andrei Alexandrescu wrote: On 3/23/12 2:28 PM, Timon Gehr wrote: On 03/23/2012 08:06 PM, Andrei Alexandrescu wrote: Casting is very different from to, and useless for your purposes. You must use to. Andrei druntime mustn't depend on Phobos, and I don't see why it is

Re: Implicit conversions for AA keys

2012-03-23 Thread Andrei Alexandrescu
On 3/23/12 3:23 PM, Timon Gehr wrote: On 03/23/2012 09:05 PM, Andrei Alexandrescu wrote: On 3/23/12 2:28 PM, Timon Gehr wrote: On 03/23/2012 08:06 PM, Andrei Alexandrescu wrote: Casting is very different from to, and useless for your purposes. You must use to. Andrei druntime mustn't depe

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
On 03/23/2012 09:43 PM, Andrei Alexandrescu wrote: On 3/23/12 3:23 PM, Timon Gehr wrote: On 03/23/2012 09:05 PM, Andrei Alexandrescu wrote: On 3/23/12 2:28 PM, Timon Gehr wrote: On 03/23/2012 08:06 PM, Andrei Alexandrescu wrote: Casting is very different from to, and useless for your purpose

Re: Implicit conversions for AA keys

2012-03-23 Thread Timon Gehr
On 03/23/2012 10:07 PM, Timon Gehr wrote: I see. An alternative solution (one that does not make AAs depend on Phobos and is more slick) would be to use the const qualified key type for lookup (that is what const is for) and to have immutable keys for stores. For types that define .idup, there w

Re: Implicit conversions for AA keys

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 10:53:10PM +0100, Timon Gehr wrote: > On 03/23/2012 10:07 PM, Timon Gehr wrote: > > > >I see. An alternative solution (one that does not make AAs depend on > >Phobos and is more slick) would be to use the const qualified key type > >for lookup (that is what const is for) and

Re: Implicit conversions for AA keys

2012-03-24 Thread Timon Gehr
On 03/24/2012 01:20 AM, H. S. Teoh wrote: On Fri, Mar 23, 2012 at 10:53:10PM +0100, Timon Gehr wrote: On 03/23/2012 10:07 PM, Timon Gehr wrote: I see. An alternative solution (one that does not make AAs depend on Phobos and is more slick) would be to use the const qualified key type for lookup

Re: Implicit conversions for AA keys

2012-03-24 Thread Timon Gehr
On 03/24/2012 12:34 PM, Timon Gehr wrote: else static if(is(T _ == shared(U),U)) alias const(getConstQual!U) r; should be else static if(is(T _ == shared(U),U)) alias shared(const(getConstQual!U)) r; But it does not matter for the AA implementation because it only uses getConstQual with im

Re: Implicit conversions for AA keys

2012-03-24 Thread Timon Gehr
On 03/24/2012 12:34 PM, Timon Gehr wrote: static if(is(typeof(getConstQual!Key.init.idup):Key)){ Better: static if(is(typeof(getConstQual!Key.init.idup):Key) && !is(getConstQual!Key: Key)){

Re: Implicit conversions for AA keys

2012-03-24 Thread H. S. Teoh
On Sat, Mar 24, 2012 at 12:34:56PM +0100, Timon Gehr wrote: > On 03/24/2012 01:20 AM, H. S. Teoh wrote: [...] > >Hmm. I decided that perhaps the full-fledged std.conv.to is a bit of > >an overkill, so I revised the AA code to compromise between needing > >std.conv.to and still deliver what Andrei w

Re: Implicit conversions for AA keys

2012-03-24 Thread H. S. Teoh
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 im

Re: Implicit conversions for AA keys

2012-03-24 Thread Timon Gehr
On 03/24/2012 05:14 PM, H. S. Teoh wrote: 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