Jump to page: 1 2
Thread overview
appending newly initialized struct to array
Apr 17, 2012
maarten van damme
Apr 17, 2012
simendsjo
Apr 17, 2012
bearophile
Apr 18, 2012
Kenji Hara
Apr 18, 2012
Ali Çehreli
Apr 18, 2012
Kenji Hara
Apr 18, 2012
maarten van damme
Apr 18, 2012
Somedude
Apr 18, 2012
Ali Çehreli
Apr 18, 2012
bearophile
Apr 18, 2012
SomeDude
Apr 18, 2012
Jonathan M Davis
Apr 18, 2012
Ali Çehreli
Apr 18, 2012
SomeDude
Apr 18, 2012
H. S. Teoh
Apr 18, 2012
bearophile
Apr 18, 2012
Jonathan M Davis
Apr 19, 2012
bearophile
April 17, 2012
Just for fun I decided to complete some codejam challenges in D. At some point I wanted to add structs to an array but I got a compiler error. What am I doing wrong?

code:
struct test{
int x;
int y;
}
void main(){
test[] why;
why~={3,5};
}

error:
wait.d(7): found '}' when expecting ';' following statement
wait.d(8): found 'EOF' when expecting ';' following statement
wait.d(8): found 'EOF' when expecting '}' following compound statement

Is there any reason a why this wouldn't work?


April 17, 2012
On Tue, 17 Apr 2012 22:28:31 +0200, maarten van damme <maartenvd1994@gmail.com> wrote:

> Just for fun I decided to complete some codejam challenges in D. At some
> point I wanted to add structs to an array but I got a compiler error. What
> am I doing wrong?
>
> code:
> struct test{
> int x;
> int y;
> }
> void main(){
> test[] why;
> why~={3,5};
> }
>
> error:
> wait.d(7): found '}' when expecting ';' following statement
> wait.d(8): found 'EOF' when expecting ';' following statement
> wait.d(8): found 'EOF' when expecting '}' following compound statement
>
> Is there any reason a why this wouldn't work?

Sounds like a bug. C style initializers work in other cases:

struct S { int i; }
void main() {
    S[] arr;
    S s = { 1 };
    arr ~= S(1);
    // But the following barfs
    //arr ~= { 1 };
    //arr ~= { i:1 };
    //arr[0] = { 1 };
}
April 17, 2012
simendsjo:

> Sounds like a bug. C style initializers work in other cases:

D language is so much irregular, so many special cases that don't work :-)

Bye,
bearophile
April 18, 2012
On Tuesday, 17 April 2012 at 21:00:55 UTC, simendsjo wrote:
> On Tue, 17 Apr 2012 22:28:31 +0200, maarten van damme <maartenvd1994@gmail.com> wrote:
>
>> Just for fun I decided to complete some codejam challenges in D. At some
>> point I wanted to add structs to an array but I got a compiler error. What
>> am I doing wrong?
>>
>> code:
>> struct test{
>> int x;
>> int y;
>> }
>> void main(){
>> test[] why;
>> why~={3,5};
>> }
>>
>> error:
>> wait.d(7): found '}' when expecting ';' following statement
>> wait.d(8): found 'EOF' when expecting ';' following statement
>> wait.d(8): found 'EOF' when expecting '}' following compound statement
>>
>> Is there any reason a why this wouldn't work?
>
> Sounds like a bug. C style initializers work in other cases:
>
> struct S { int i; }
> void main() {
>     S[] arr;
>     S s = { 1 };
>     arr ~= S(1);
>     // But the following barfs
>     //arr ~= { 1 };
>     //arr ~= { i:1 };
>     //arr[0] = { 1 };
> }

No, it is designed. {3,5} is struct initializer:
http://dlang.org/declaration.html#StructInitializer
And it is only allowed in initializer of variable declarations.

> why~={3,5};

This is concat assign expression, so you should use test(3,5)
instead of {3,5}. That is StructLiteral:
http://dlang.org/struct.html#StructLiteral
and it is an expression.

Bye.

Kenji Hara
April 18, 2012
On 04/17/2012 02:00 PM, simendsjo wrote:

> Sounds like a bug. C style initializers work in other cases:

I try not to use them. I think they have this 'feature' of leaving unspecified members uninitialized:

struct S
{
    int i;
    double d;
}

void main()
{
    S s = { 42 };               // <-- no initializer for S.d
    assert(s.i == 42);
    assert(s.d == double.nan);  // <-- fails (may work for you)
}

Is that a bug or a feature? I might have opened it but I don't remember now. :)

Ali

April 18, 2012
On Wednesday, 18 April 2012 at 04:55:23 UTC, Ali Çehreli wrote:
> On 04/17/2012 02:00 PM, simendsjo wrote:
>
> > Sounds like a bug. C style initializers work in other cases:
>
> I try not to use them. I think they have this 'feature' of leaving unspecified members uninitialized:
>
> struct S
> {
>     int i;
>     double d;
> }
>
> void main()
> {
>     S s = { 42 };               // <-- no initializer for S.d
>     assert(s.i == 42);
>     assert(s.d == double.nan);  // <-- fails (may work for you)

You should use std.math.isNaN whether a floating point value is NaN.

      assert(isNaN(s.d));  // <-- success

> }
>
> Is that a bug or a feature? I might have opened it but I don't remember now. :)
>
> Ali

Bye.

Kenji Hara
April 18, 2012
That's a very odd design. Making it work when instantiating a new struct of that type but not inline. Anyway, test(3,5) works perfect, thank you.


April 18, 2012
Le 18/04/2012 12:41, maarten van damme a écrit :
> That's a very odd design. Making it work when instantiating a new struct of that type but not inline. Anyway, test(3,5) works perfect, thank you.

It's not odd at all. You append a structure, not an array.
{3,5} is for array initialization, it's the same syntax as in C, C++,
Java, C#.

What if you want to append an array of structures ?

    why~=[test(3,5), test(3,6)];
April 18, 2012
On 04/18/2012 12:16 AM, Kenji Hara wrote:
> On Wednesday, 18 April 2012 at 04:55:23 UTC, Ali Çehreli wrote:

>> assert(s.d == double.nan); // <-- fails (may work for you)
>
> You should use std.math.isNaN whether a floating point value is NaN.
>
> assert(isNaN(s.d)); // <-- success

That a thousandth time I have made that mistake and still have not learned. :( Yes, .nan may not be compared with any other value, including .nan.

Ali
April 18, 2012
Ali:
> That a thousandth time I have made that mistake and still have not learned. :( Yes, .nan may not be compared with any other value, including .nan.

Today I'll present an enhancement request to remove this problem from D.

Hugs,
bearophile
« First   ‹ Prev
1 2