Implicit string lit conversion to wstring/dstring

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Mar 14 12:46:46 PDT 2012


On Wed, Mar 14, 2012 at 02:07:04PM -0500, Andrei Alexandrescu wrote:
> On 3/14/12 2:01 PM, H. S. Teoh wrote:
> >However, this change broke this code:
> >
> >	AssociativeArray!(wstring,int) aa;
> >	aa["abc"] = 123;	// error: compiler deduces K as string,
> >				// so isCompatWithKey!K fails: string
> >				// can't implicitly convert to wstring
> >
> >Whereas before, when opIndexAssign looked like this:
> >
> >		void opIndexAssign(in Value v, in Key key)
> >		{
> >			...
> >		}
> >
> >everything worked, because the compiler deduces the type of "abc" as
> >wstring since Key==wstring.
> 
> Aha! This is one of those cases in which built-in magic smells of
> putrid beef soup.
> 
> I think it's possible to still make this work by beefing up the
> template constraints such that the working signature is selected for
> strings.
[...]

Also, IMHO, this needs to work for array literals in general, not just
strings. For example, this should work:

	int[ubyte[]] aa;
	aa[[1,2,3]] = 123;

The [1,2,3] should be deduced as ubyte[] instead of int[].


T

-- 
Ignorance is bliss... but only until you suffer the consequences!


More information about the Digitalmars-d mailing list