Thread overview
Re: Wrong lowering for a[b][c]++
Mar 23, 2012
H. S. Teoh
Mar 23, 2012
Ed McCardell
Mar 23, 2012
Daniel Murphy
Mar 23, 2012
Andrej Mitrovic
Mar 23, 2012
Timon Gehr
Mar 23, 2012
Andrej Mitrovic
Mar 23, 2012
Timon Gehr
Mar 23, 2012
Andrej Mitrovic
Mar 23, 2012
bearophile
Mar 23, 2012
James Miller
March 23, 2012
On Fri, Mar 23, 2012 at 06:11:05AM +0100, Andrej Mitrovic wrote: [...]
> Btw, want to see a magic trick? Put this into your hash:
> 
> this(AA)(AA aa)
>     if (std.traits.isAssociativeArray!AA
>         && is(KeyType!AA == keytype)
>         && is(ValueType!AA == valuetype))
> {
>     foreach (key, val; aa)
>         this[key] = val;
> }
> 
> And theeeen..... *drumroll*:
> 
> AA!(string,int) bb = cast()["abc":123];
> 
> badoom-tshhh. LOL!

WAT?! What on earth is "cast()" supposed to mean?? That's just screwed up. Well anyway, I pushed the change to github, since it at least makes literal sliiightly more usable. Have fun! :-)


T

-- 
What's a "hot crossed bun"? An angry rabbit.
March 23, 2012
On 3/23/12, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> WAT?! What on earth is "cast()" supposed to mean??

I've no idea. It's probably a front-end bug and the cast forces the compiler to.. come to its senses?
March 23, 2012
On 23 March 2012 19:15, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> I've no idea. It's probably a front-end bug and the cast forces the compiler to.. come to its senses?

`cast()` is the compiler equivalent to a slap with a wet fish?

--
James Miller
March 23, 2012
On 03/23/2012 01:24 AM, H. S. Teoh wrote:
> WAT?! What on earth is "cast()" supposed to mean??

I think it removes one level of const or immutable from the type of its argument. Why that helps in this case, I don't know.

--Ed
March 23, 2012
"H. S. Teoh" <hsteoh@quickfur.ath.cx> wrote in message news:mailman.1036.1332480215.4860.digitalmars-d@puremagic.com...
> On Fri, Mar 23, 2012 at 06:11:05AM +0100, Andrej Mitrovic wrote: [...]
>> Btw, want to see a magic trick? Put this into your hash:
>>
>> this(AA)(AA aa)
>>     if (std.traits.isAssociativeArray!AA
>>         && is(KeyType!AA == keytype)
>>         && is(ValueType!AA == valuetype))
>> {
>>     foreach (key, val; aa)
>>         this[key] = val;
>> }
>>
>> And theeeen..... *drumroll*:
>>
>> AA!(string,int) bb = cast()["abc":123];
>>
>> badoom-tshhh. LOL!
>
> WAT?! What on earth is "cast()" supposed to mean?? That's just screwed up. Well anyway, I pushed the change to github, since it at least makes literal sliiightly more usable. Have fun! :-)
>

My guess is that cast forces it to be parsed as an ExpInitializer instead of an ArrayInitializer.  Associative array literals as initializers are parsed as array initializers then reinterpreted during semantic.

And cast() is like cast(const) or cast(shared).


March 23, 2012
On 03/23/2012 07:15 AM, Andrej Mitrovic wrote:
> On 3/23/12, H. S. Teoh<hsteoh@quickfur.ath.cx>  wrote:
>> WAT?! What on earth is "cast()" supposed to mean??
>
> I've no idea. It's probably a front-end bug and the cast forces the
> compiler to.. come to its senses?

That part is not a bug, it is specified.

http://dlang.org/expression.html#CastExpression

"Casting with no Type or CastQual removes any top level const, immutable, shared or inout type modifiers from the type of the UnaryExpression."

What is a bug is that array initializers cannot be used to initialize structs through associative array alias this.

Maybe you are also missing that this is valid code:

int[] a = [1 : 2, 3 : 4];

This only works for initializers of the form [ ... ]. The cast() removes that possibility.

This is why the compiler gets confused.

March 23, 2012
On 3/23/12, Timon Gehr <timon.gehr@gmx.ch> wrote:
> Maybe you are also missing that this is valid code:
> int[] a = [1 : 2, 3 : 4];

What is this syntax for and how is it used? It creates '[0, 2, 0, 4]', which is puzzling to me.
March 23, 2012
On 03/23/2012 09:10 PM, Andrej Mitrovic wrote:
> On 3/23/12, Timon Gehr<timon.gehr@gmx.ch>  wrote:
>> Maybe you are also missing that this is valid code:
>> int[] a = [1 : 2, 3 : 4];
>
> What is this syntax for and how is it used? It creates '[0, 2, 0, 4]',
> which is puzzling to me.

It creates an array from key-value pairs. a[1] will be 2 and a[3] will be 4. Unspecified entries are default-initialized. It can be quite useful for building lookup tables.

Another reason why array initializers are different from array literals:
struct S{int a,b,c;}
S[] sarr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}];
March 23, 2012
On 3/23/12, Timon Gehr <timon.gehr@gmx.ch> wrote:
> It creates an array from key-value pairs. a[1] will be 2 and a[3] will be 4. Unspecified entries are default-initialized. It can be quite useful for building lookup tables.

Interesting. It's documented under "Static Initialization of Statically Allocated Arrays", but I guess it works for dynamic arrays too. I could use these for sure. :)

> Another reason why array initializers are different from array literals:
> struct S{int a,b,c;}
> S[] sarr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}];

Yeah I knew about those, although IIRC these might be deprecated?
March 23, 2012
Andrej Mitrovic:

> On 3/23/12, Timon Gehr <timon.gehr@gmx.ch> wrote:
> > Maybe you are also missing that this is valid code:
> > int[] a = [1 : 2, 3 : 4];
> 
> What is this syntax for and how is it used? It creates '[0, 2, 0, 4]', which is puzzling to me.

See:
http://d.puremagic.com/issues/show_bug.cgi?id=4703

This is why I was unnerved when Walter has recently said that we should reduce the amount of breaking changes in D. There are several D problems like that one that should be fixed. This is an example of bug report that needs to be addressed sooner instead of later.

Bye,
bearophile