Jump to page: 1 2
Thread overview
Static array .init function does not work
Feb 25, 2005
Derek Parnell
Feb 25, 2005
Ben Hinkle
Feb 25, 2005
Stewart Gordon
Feb 25, 2005
Ben Hinkle
Feb 26, 2005
kris
Mar 18, 2005
Thomas Kuehne
Mar 18, 2005
Stewart Gordon
Mar 18, 2005
Thomas Kuehne
Mar 18, 2005
Stewart Gordon
Mar 18, 2005
Thomas Kuehne
Mar 18, 2005
Stewart Gordon
Mar 19, 2005
Regan Heath
Mar 21, 2005
Stewart Gordon
Walter's input desired, was (Re: Static array .init function does not work)
Mar 21, 2005
Regan Heath
Mar 21, 2005
Stewart Gordon
Mar 21, 2005
Regan Heath
February 25, 2005
import std.stdio;

void main()
{
 static int[2] a;
 a[0]=5;
 a[1]=10;
 a.init;
 writefln(a[0],",",a[1]);
}

Prints 5,10.  Remove "static" and it prints 0,0 as expected.

Also happens with class static arrays.


February 25, 2005
On Thu, 24 Feb 2005 22:56:53 -0500, Jarrett Billingsley wrote:

> import std.stdio;
> 
> void main()
> {
>  static int[2] a;
>  a[0]=5;
>  a[1]=10;
>  a.init;
>  writefln(a[0],",",a[1]);
> }
> 
> Prints 5,10.  Remove "static" and it prints 0,0 as expected.
> 
> Also happens with class static arrays.

Confirmed. Here is another example ...

<code>
 import std.stdio;

 void main()
 {
  static int a;
         int b;
  a=5;
  b=5;
  writefln("before a=%d b=%d", a, b);
  a.init;
  b.init;
  writefln(" after a=%d b=%d", a, b);
 }

</code>
-- 
Derek
Melbourne, Australia
25/02/2005 3:10:14 PM
February 25, 2005
"Derek Parnell" <derek@psych.ward> wrote in message news:16xo8j2vhhc9z$.1ub0088au56os$.dlg@40tude.net...
> On Thu, 24 Feb 2005 22:56:53 -0500, Jarrett Billingsley wrote:
>
>> import std.stdio;
>>
>> void main()
>> {
>>  static int[2] a;
>>  a[0]=5;
>>  a[1]=10;
>>  a.init;
>>  writefln(a[0],",",a[1]);
>> }
>>
>> Prints 5,10.  Remove "static" and it prints 0,0 as expected.
>>
>> Also happens with class static arrays.
>
> Confirmed. Here is another example ...
>
> <code>
> import std.stdio;
>
> void main()
> {
>  static int a;
>         int b;
>  a=5;
>  b=5;
>  writefln("before a=%d b=%d", a, b);
>  a.init;
>  b.init;
>  writefln(" after a=%d b=%d", a, b);
> }
>
> </code>
> -- 
> Derek
> Melbourne, Australia
> 25/02/2005 3:10:14 PM

interesting. I didn't know b.init would change b. I thought it would just
evaluate to b's initializer. To quote the spec:
.init produces a constant expression that is the default initializer. If
applied to a type, it is the default initializer for that type. If applied
to a variable or field, it is the default initializer for that variable or
field.

Walter, can you make the doc more clear that b.init will change b? I assume the way to get the initializer for b without changing b is to do typeof(b).init? I'm not sure how this works with fields, though, since the initializer for the field depends on exactly which field you are talking about and not just the type of the field.


February 25, 2005
Ben Hinkle wrote:
> "Derek Parnell" <derek@psych.ward> wrote in message news:16xo8j2vhhc9z$.1ub0088au56os$.dlg@40tude.net...
> 
>>On Thu, 24 Feb 2005 22:56:53 -0500, Jarrett Billingsley wrote:
>>
>>
>>>import std.stdio;
>>>
>>>void main()
>>>{
>>> static int[2] a;
>>> a[0]=5;
>>> a[1]=10;
>>> a.init;
>>> writefln(a[0],",",a[1]);
>>>}
>>>
>>>Prints 5,10.  Remove "static" and it prints 0,0 as expected.

Why expected?  .init is a property which, AIUI, isn't supposed to have any side effect.

<snip>
> interesting. I didn't know b.init would change b. I thought it would just evaluate to b's initializer. To quote the spec:
> .init produces a constant expression that is the default initializer. If applied to a type, it is the default initializer for that type. If applied to a variable or field, it is the default initializer for that variable or field.
> 
> Walter, can you make the doc more clear that b.init will change b?

Only if that's the intended behaviour, which it most certainly isn't. Having .init do two completely different things depending on what it's applied to is no good for anyone, and I doubt this is among the kinds of warts that Walter would put into the language.

> I assume the way to get the initializer for b without changing b is to do typeof(b).init?

No.

The initialiser of a variable and the initialiser of a type are two different concepts.  Suppose you have

    static int[2] a = [ 13, 42 ];
    a[0] = 69;
    writefln(a[0],",",a[1]);
    writefln(a.init[0],",",a.init[1]);
    writefln(typeof(a).init[0],",",typeof(a).init[1]);
    writefln(a[0],",",a[1]);

then the output should be

69,42
13,42
0,0
69,42

whereas with what you're making out it would be

69,42
13,42
13,42
13,42

> I'm not sure how this works with fields, though, since the initializer for the field depends on exactly which field you are talking about and not just the type of the field.

    struct Qwert {
        int yuiop = 105;
    }

    void main() {
        Qwert asdfg = { yuiop: 23 };
        writefln(Qwert.yuiop.init);
        writefln(asdfg.yuiop.init);
    }

should output

105
23

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
February 25, 2005
"Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:cvmu8n$der$1@digitaldaemon.com...
> Ben Hinkle wrote:
>> "Derek Parnell" <derek@psych.ward> wrote in message news:16xo8j2vhhc9z$.1ub0088au56os$.dlg@40tude.net...
>>
>>>On Thu, 24 Feb 2005 22:56:53 -0500, Jarrett Billingsley wrote:
>>>
>>>
>>>>import std.stdio;
>>>>
>>>>void main()
>>>>{
>>>> static int[2] a;
>>>> a[0]=5;
>>>> a[1]=10;
>>>> a.init;
>>>> writefln(a[0],",",a[1]);
>>>>}
>>>>
>>>>Prints 5,10.  Remove "static" and it prints 0,0 as expected.
>
> Why expected?  .init is a property which, AIUI, isn't supposed to have any side effect.
>
> <snip>
>> interesting. I didn't know b.init would change b. I thought it would just
>> evaluate to b's initializer. To quote the spec:
>> .init produces a constant expression that is the default initializer. If
>> applied to a type, it is the default initializer for that type. If
>> applied to a variable or field, it is the default initializer for that
>> variable or field.
>>
>> Walter, can you make the doc more clear that b.init will change b?
>
> Only if that's the intended behaviour, which it most certainly isn't. Having .init do two completely different things depending on what it's applied to is no good for anyone, and I doubt this is among the kinds of warts that Walter would put into the language.

agreed. I'd prefer the spec behavior over the current compiler behavior if given the choice.


February 26, 2005
Stewart Gordon wrote:
> Having .init do two completely different things depending on what it's applied to is no good for anyone

Well said. This needs to be fixed.
March 18, 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am Fri, 25 Feb 2005 10:22:15 +0000:
> Ben Hinkle wrote:
>> "Derek Parnell" <derek@psych.ward> wrote in message news:16xo8j2vhhc9z$.1ub0088au56os$.dlg@40tude.net...
>> 
>>>On Thu, 24 Feb 2005 22:56:53 -0500, Jarrett Billingsley wrote:
>>>
>>>
>>>>import std.stdio;
>>>>
>>>>void main()
>>>>{
>>>> static int[2] a;
>>>> a[0]=5;
>>>> a[1]=10;
>>>> a.init;
>>>> writefln(a[0],",",a[1]);
>>>>}
>>>>
>>>>Prints 5,10.  Remove "static" and it prints 0,0 as expected.
>
> Why expected?  .init is a property which, AIUI, isn't supposed to have any side effect.
>
><snip>
>> interesting. I didn't know b.init would change b. I thought it would just
>> evaluate to b's initializer. To quote the spec:
>> .init produces a constant expression that is the default initializer. If
>> applied to a type, it is the default initializer for that type. If applied
>> to a variable or field, it is the default initializer for that variable or
>> field.
>> 
>> Walter, can you make the doc more clear that b.init will change b?
>
> Only if that's the intended behaviour, which it most certainly isn't. Having .init do two completely different things depending on what it's applied to is no good for anyone, and I doubt this is among the kinds of warts that Walter would put into the language.

Added to DStress as
http://dstress.kuehne.cn/run/init_01.d
http://dstress.kuehne.cn/run/init_02.d

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFCOojn3w+/yD4P9tIRApGIAJ9uz8sSCeE0VjMvHQNy8ceAvu2OSwCfZGHx
nUgCo48l2KQEfV2p4BZKouo=
=rv4+
-----END PGP SIGNATURE-----
March 18, 2005
Thomas Kuehne wrote:
<snip>
> Added to DStress as
> http://dstress.kuehne.cn/run/init_01.d
> http://dstress.kuehne.cn/run/init_02.d

This line looks a little odd to me:

    assert(array.init==int.init);

Aren't these incompatible types (int[2] and int)?

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
March 18, 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am Fri, 18 Mar 2005 11:01:43 +0000:
> Thomas Kuehne wrote:
><snip>
>> Added to DStress as
>> http://dstress.kuehne.cn/run/init_01.d
>> http://dstress.kuehne.cn/run/init_02.d
>
> This line looks a little odd to me:
>
>      assert(array.init==int.init);
>
> Aren't these incompatible types (int[2] and int)?

# void main(){
#        int[2] array;
#        printf("%.*s\n", typeid(typeof(array.init)).toString());
# }

prints: int

Thomas

-----BEGIN PGP SIGNATURE-----

iD8DBQFCOrd73w+/yD4P9tIRAvLfAKCpTnFRvLq9703Pd+SqrwklKImPngCcD2PQ
GvXCxy48Gxz8bfR3c+IssPk=
=jiMv
-----END PGP SIGNATURE-----
March 18, 2005
Thomas Kuehne wrote:
<snip>
>>This line looks a little odd to me:
>>
>>     assert(array.init==int.init);
>>
>>Aren't these incompatible types (int[2] and int)?
> 
> # void main(){
> #        int[2] array;
> #        printf("%.*s\n", typeid(typeof(array.init)).toString());
> # }
> 
> prints: int

That would seem to be itself a bug.

http://www.digitalmars.com/d/property.html

doesn't give any indication of arrays being a special case.  If it was, then what would array.init be given this?

    int[2] array = [3, 5];

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
« First   ‹ Prev
1 2