March 18, 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am Fri, 18 Mar 2005 12:06:03 +0000:
> 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];

Dynamic arrays are initialized as having zero elements. This isn't that usefull for .init

Static arrays are initialized with a given element count, each element
is initialized with the .init of the element's .init.
Returning the element-init of each array element is the only usefull
information the array-init can provide.
All element types are equal, their inits are equal, thus the behaviour is
legal.

Thomas

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

iD8DBQFCOtSr3w+/yD4P9tIRAjanAJ43yo+tnPS2KfJ9lOcfuND/2EaxFACcD4ex
WmNFL8VMPkc0TJf0yteUId0=
=amtH
-----END PGP SIGNATURE-----
March 18, 2005
Thomas Kuehne wrote:
<snip>
> Dynamic arrays are initialized as having zero elements.

Only if you don't tell them otherwise.

> This isn't that usefull for .init

Maybe not directly, but certainly in generic programming.

> Static arrays are initialized with a given element count, each element
> is initialized with the .init of the element's .init. Returning the element-init of each array element is the only usefull
> information the array-init can provide. 

Exactly.

> All element types are equal, their inits are equal, thus the behaviour is
> legal.

What are you referring to as "the behaviour" exactly?

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
March 19, 2005
I for one am a little confused about how it's "supposed" to work.

I can't really see any documentation on the arrays page:
http://www.digitalmars.com/d/arrays.html

Here are my test cases:

import std.stdio;

void main()
{
	static int[]  array1 = [1,2,3,4];		
	//writefln("array1.init = ",typeid(typeof(array1.init)));   //Error: array initializers as expressions are not allowed
	writefln("array1[].init type   = ",typeid(typeof(array1[].init)));
	writefln("array1[0].init type  = ",typeid(typeof(array1[0].init)));
	writefln("array1[0].init       = ",array1[0].init);
	writefln("array1.length        = ",array1.length);
	writefln("array1[].init.length = ",array1[].init.length);
	writefln("");
	
	static int[4] array2 = [5,6,7,8];		
	//writefln("array2.init = ",typeid(typeof(array2.init)));   //Error: array initializers as expressions are not allowed
	writefln("array2[].init type   = ",typeid(typeof(array2[].init)));
	writefln("array2[0].init type  = ",typeid(typeof(array2[0].init)));
	writefln("array2[0].init       = ",array2[0].init);
	writefln("array2.length        = ",array2.length);
	writefln("array2[].init.length = ",array2[].init.length);
	writefln("");
	
	int[]  array3;
	writefln("array3.init type     = ",typeid(typeof(array3.init)));
	writefln("array3[].init type   = ",typeid(typeof(array3[].init)));
	writefln("array3[0].init type  = ",typeid(typeof(array3[0].init)));
	writefln("array3[0].init       = ",array3[0].init);
	writefln("array3.length        = ",array3.length);
	writefln("array3.init.length   = ",array3.init.length);
	writefln("array3[].init.length = ",array3[].init.length);
	writefln("");
	
	int[4] array4;
	writefln("array4.init type     = ",typeid(typeof(array4.init)));
	writefln("array4[].init type   = ",typeid(typeof(array4[].init)));
	writefln("array4[0].init type  = ",typeid(typeof(array4[0].init)));
	writefln("array4[0].init       = ",array4[0].init);
	writefln("array4.length        = ",array4.length);
	//writefln("array4.init.length   = ",array4.init.length);   //no property 'length' for type 'int'
	writefln("array4[].init.length = ",array4[].init.length);
	writefln("");
}

Producing:

array1[].init type   = int[]
array1[0].init type  = int
array1[0].init       = 0
array1.length        = 4
array1[].init.length = 0

array2[].init type   = int[]
array2[0].init type  = int
array2[0].init       = 0
array2.length        = 4
array2[].init.length = 0

array3.init type     = int[]
array3[].init type   = int[]
array3[0].init type  = int
array3[0].init       = 0
array3.length        = 0
array3.init.length   = 0
array3[].init.length = 0

array4.init type     = int
array4[].init type   = int[]
array4[0].init type  = int
array4[0].init       = 0
array4.length        = 4
array4[].init.length = 0

Regan

On Fri, 18 Mar 2005 14:26:20 +0000, Stewart Gordon <smjg_1998@yahoo.com> wrote:

> Thomas Kuehne wrote:
> <snip>
>> Dynamic arrays are initialized as having zero elements.
>
> Only if you don't tell them otherwise.
>
>> This isn't that usefull for .init
>
> Maybe not directly, but certainly in generic programming.
>
>> Static arrays are initialized with a given element count, each element
>> is initialized with the .init of the element's .init. Returning the element-init of each array element is the only usefull
>> information the array-init can provide.
>
> Exactly.
>
>> All element types are equal, their inits are equal, thus the behaviour is
>> legal.
>
> What are you referring to as "the behaviour" exactly?
>
> Stewart.
>

March 21, 2005
Regan Heath wrote:
> I for one am a little confused about how it's "supposed" to work.
> 
> I can't really see any documentation on the arrays page:
> http://www.digitalmars.com/d/arrays.html

The spec gives no special case for arrays.  Hence .init works exactly the same.

> Here are my test cases:
> 
> import std.stdio;
> 
> void main()
> {
>     static int[]  array1 = [1,2,3,4];           //writefln("array1.init = ",typeid(typeof(array1.init)));   //Error:  array initializers as expressions are not allowed

If there's any correct behaviour, it's that array1.init is an int[] containing the (length, pointer) tuple to which array1 was initialised.  It follows that, if a given dynamic array's .init reference is used, then the GC must hold on to the initialisation data.  Since the array's static, this could be done by keeping the initial data in the static data segment.

>     writefln("array1[].init type   = ",typeid(typeof(array1[].init)));
>     writefln("array1[0].init type  = ",typeid(typeof(array1[0].init)));
>     writefln("array1[0].init       = ",array1[0].init);
>     writefln("array1.length        = ",array1.length);
>     writefln("array1[].init.length = ",array1[].init.length);
>     writefln("");
>         static int[4] array2 = [5,6,7,8];           //writefln("array2.init = ",typeid(typeof(array2.init)));   //Error:  array initializers as expressions are not allowed

That doesn't look right at all.  It should be an int[4] containing the constant data [5,6,7,8].

>     writefln("array2[].init type   = ",typeid(typeof(array2[].init)));
>     writefln("array2[0].init type  = ",typeid(typeof(array2[0].init)));
>     writefln("array2[0].init       = ",array2[0].init);
>     writefln("array2.length        = ",array2.length);
>     writefln("array2[].init.length = ",array2[].init.length);
>     writefln("");
>         int[]  array3;
>     writefln("array3.init type     = ",typeid(typeof(array3.init)));
>     writefln("array3[].init type   = ",typeid(typeof(array3[].init)));
>     writefln("array3[0].init type  = ",typeid(typeof(array3[0].init)));
>     writefln("array3[0].init       = ",array3[0].init);
>     writefln("array3.length        = ",array3.length);
>     writefln("array3.init.length   = ",array3.init.length);
>     writefln("array3[].init.length = ",array3[].init.length);
>     writefln("");
>         int[4] array4;
>     writefln("array4.init type     = ",typeid(typeof(array4.init)));
>     writefln("array4[].init type   = ",typeid(typeof(array4[].init)));
>     writefln("array4[0].init type  = ",typeid(typeof(array4[0].init)));
>     writefln("array4[0].init       = ",array4[0].init);
>     writefln("array4.length        = ",array4.length);
>     //writefln("array4.init.length   = ",array4.init.length);   //no property  'length' for type 'int'

This is wrong too.

>     writefln("array4[].init.length = ",array4[].init.length);
>     writefln("");
> }
> 
> Producing:
> 
> array1[].init type   = int[]
> array1[0].init type  = int
> array1[0].init       = 0

I guess it's debatable whether it makes sense to talk of the initialiser of an element or slice of a dynamic array.  But the result is certainly wrong.

Intuitively, it could defined such that array1[0].init == array1.init[0], which would be 1 in this case.

> array1.length        = 4
> array1[].init.length = 0

I don't get quite what DMD is making of this.  But again, whatever it's doing is certainly bogus.

> array2[].init type   = int[]
> array2[0].init type  = int
> array2[0].init       = 0
> array2.length        = 4
> array2[].init.length = 0

Since static arrays are contained by value, their elements/slices are in effect variables in their own right, so it follows that all should be valid.  But again, the zeros are wrong.

> array3.init type     = int[]
> array3[].init type   = int[]
> array3[0].init type  = int
> array3[0].init       = 0
> array3.length        = 0
> array3.init.length   = 0
> array3[].init.length = 0
<snip>

These should behave in precisely the same ways as for the static case. But the fact that the array's initialiser is null in this case makes for a few differences.  For once, array3.init.length is reported correctly, but array3[0].init should throw an ArrayBoundsError if it should be allowed at all.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
March 21, 2005
On Mon, 21 Mar 2005 11:11:24 +0000, Stewart Gordon <smjg_1998@yahoo.com> wrote:
> Regan Heath wrote:
>> I for one am a little confused about how it's "supposed" to work.
>>  I can't really see any documentation on the arrays page:
>> http://www.digitalmars.com/d/arrays.html
>
> The spec gives no special case for arrays.  Hence .init works exactly the same.
>
>> Here are my test cases:
>>  import std.stdio;
>>  void main()
>> {
>>     static int[]  array1 = [1,2,3,4];           //writefln("array1.init = ",typeid(typeof(array1.init)));   //Error:  array initializers as expressions are not allowed
>
> If there's any correct behaviour, it's that array1.init is an int[] containing the (length, pointer) tuple to which array1 was initialised.

Indeed. I was beginning to suspect at this point that DMD was replacing "array1.init" with "[1,2,3,4]" so my line:
  typeid(typeof(array2.init))

actually reads:
  typeid(typeof([5,6,7,8]))

which is why it gives the error "array initializers as expressions are not allowed"

This seems to be supported by the docs:
file://localhost/C:/Library/D/dmd/html/d/property.html
".init produces a constant expression that is the default initializer."

Walter can you verify/deny this and/or explain the error message?


>   It follows that, if a given dynamic array's .init reference is used, then the GC must hold on to the initialisation data.  Since the array's static, this could be done by keeping the initial data in the static data segment.

This is what I expected it to do, it appears to instead replace x.init with "a constant expression that is the default initializer."


>>     writefln("array2[].init type   = ",typeid(typeof(array2[].init)));
>>     writefln("array2[0].init type  = ",typeid(typeof(array2[0].init)));
>>     writefln("array2[0].init       = ",array2[0].init);
>>     writefln("array2.length        = ",array2.length);
>>     writefln("array2[].init.length = ",array2[].init.length);
>>     writefln("");
>>         int[]  array3;
>>     writefln("array3.init type     = ",typeid(typeof(array3.init)));
>>     writefln("array3[].init type   = ",typeid(typeof(array3[].init)));
>>     writefln("array3[0].init type  = ",typeid(typeof(array3[0].init)));
>>     writefln("array3[0].init       = ",array3[0].init);
>>     writefln("array3.length        = ",array3.length);
>>     writefln("array3.init.length   = ",array3.init.length);
>>     writefln("array3[].init.length = ",array3[].init.length);
>>     writefln("");
>>         int[4] array4;
>>     writefln("array4.init type     = ",typeid(typeof(array4.init)));
>>     writefln("array4[].init type   = ",typeid(typeof(array4[].init)));
>>     writefln("array4[0].init type  = ",typeid(typeof(array4[0].init)));
>>     writefln("array4[0].init       = ",array4[0].init);
>>     writefln("array4.length        = ",array4.length);
>>     //writefln("array4.init.length   = ",array4.init.length);   //no property  'length' for type 'int'
>
> This is wrong too.

Indeed. It seems to think array4.init is an 'int'. I would have expected an int[], or null reference.

It could be argued that "int[4] array4" is the same as "int[4] array4 = 0", as arrays are initialised to 0. In which case the init value "0" is technically an 'int'.


>>     writefln("array4[].init.length = ",array4[].init.length);
>>     writefln("");
>> }
>>  Producing:
>>  array1[].init type   = int[]
>> array1[0].init type  = int
>> array1[0].init       = 0
>
> I guess it's debatable whether it makes sense to talk of the initialiser of an element or slice of a dynamic array.

Maybe. I wouldn't rule it out, someone is bound to think of a use for it. :)


> But the result is certainly wrong.

Indeed.


> Intuitively, it could defined such that array1[0].init == array1.init[0], which would be 1 in this case.

Agreed.


>> array1.length        = 4
>> array1[].init.length = 0
>
> I don't get quite what DMD is making of this.  But again, whatever it's doing is certainly bogus.

It makes no sense to me :)


>> array2[].init type   = int[]
>> array2[0].init type  = int
>> array2[0].init       = 0
>> array2.length        = 4
>> array2[].init.length = 0
>
> Since static arrays are contained by value, their elements/slices are in effect variables in their own right, so it follows that all should be valid.  But again, the zeros are wrong.

Perhaps it is simply returning the .init value of the 'type' involved i.e. 'int'. Rather than the .init value of the instance of the int?


>> array3.init type     = int[]
>> array3[].init type   = int[]
>> array3[0].init type  = int
>> array3[0].init       = 0
>> array3.length        = 0
>> array3.init.length   = 0
>> array3[].init.length = 0
> <snip>
>
> These should behave in precisely the same ways as for the static case. But the fact that the array's initialiser is null in this case makes for a few differences.  For once, array3.init.length is reported correctly,

I think that's a fluke given the previous behaviour :)


> but array3[0].init should throw an ArrayBoundsError if it should be allowed at all.

Indeed. I expected the bounds error.


Regan
March 21, 2005
Regan Heath wrote:
<snip>
> Indeed. I was beginning to suspect at this point that DMD was replacing  "array1.init" with "[1,2,3,4]" so my line:
>   typeid(typeof(array2.init))
> 
> actually reads:
>   typeid(typeof([5,6,7,8]))
> 
> which is why it gives the error "array initializers as expressions are not  allowed"
> 
> This seems to be supported by the docs:
> file://localhost/C:/Library/D/dmd/html/d/property.html

None of us can access the files on your hard drive.

<snip>
> It could be argued that "int[4] array4" is the same as "int[4] array4 =  0", as arrays are initialised to 0. In which case the init value "0" is  technically an 'int'.

Obviously 0 is an int.  But we can't sensibly have the type of .init dependent on the syntax that was used to initialise it.  All type.init properties should be of that type, and all variable.init properties should be the same type as the variable.

<snip>
>> Since static arrays are contained by value, their elements/slices are in  effect variables in their own right, so it follows that all should be  valid.  But again, the zeros are wrong.
> 
> Perhaps it is simply returning the .init value of the 'type' involved i.e.  'int'. Rather than the .init value of the instance of the int?
<snip>

Yes, that is probably the essence of the bug.  We could experiment with it further.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
March 21, 2005
On Mon, 21 Mar 2005 11:53:54 +0000, Stewart Gordon <smjg_1998@yahoo.com> wrote:
> Regan Heath wrote:
> <snip>
>> Indeed. I was beginning to suspect at this point that DMD was replacing  "array1.init" with "[1,2,3,4]" so my line:
>>   typeid(typeof(array2.init))
>>  actually reads:
>>   typeid(typeof([5,6,7,8]))
>>  which is why it gives the error "array initializers as expressions are not  allowed"
>>  This seems to be supported by the docs:
>> file://localhost/C:/Library/D/dmd/html/d/property.html
>
> None of us can access the files on your hard drive.

LOL! <mumble>.. <mutter>.. <local copy>..
http://www.digitalmars.com/d/property.html

> <snip>
>> It could be argued that "int[4] array4" is the same as "int[4] array4 =  0", as arrays are initialised to 0. In which case the init value "0" is  technically an 'int'.
>
> Obviously 0 is an int.  But we can't sensibly have the type of .init dependent on the syntax that was used to initialise it.  All type.init properties should be of that type, and all variable.init properties should be the same type as the variable.

I agree.. I was playing devils advocate again.

> <snip>
>>> Since static arrays are contained by value, their elements/slices are in  effect variables in their own right, so it follows that all should be  valid.  But again, the zeros are wrong.
>>  Perhaps it is simply returning the .init value of the 'type' involved i.e.  'int'. Rather than the .init value of the instance of the int?
> <snip>
>
> Yes, that is probably the essence of the bug.  We could experiment with it further.

I rather hoped to attract Walters attention and save us the trouble.

Regan
1 2
Next ›   Last »