www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nice feature, but...

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
In D I can declare new POD type with default value
like

typedef uint symbol_t = 0xFFFFFFFF;

This is just perfect and almost works.
But not here:

symbol_t[char[]] symbols;
symbol_t v = symbols["arg"];

In ideal world 'v' should have 0xFFFFFFFF value
but it has 0 instead. Is this so by design?

Andrew. 
Apr 06 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
 typedef uint symbol_t = 0xFFFFFFFF;
 symbol_t[char[]] symbols;
 symbol_t v = symbols["arg"];

 In ideal world 'v' should have 0xFFFFFFFF value
 but it has 0 instead. Is this so by design?

I think this is a bug in AA's and dynamic arrays resized by setting their length property - the memory is filled with 0 instead of the default value. So floating point values are filled with 0 instead of NaN and strings are filled with 0 instead of 0xFF. If you allocate a dynamic array using "new" it will get the right default values.
Apr 06 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 typedef uint symbol_t = 0xFFFFFFFF;
 symbol_t[char[]] symbols;
 symbol_t v = symbols["arg"];

 In ideal world 'v' should have 0xFFFFFFFF value
 but it has 0 instead. Is this so by design?

I think this is a bug in AA's and dynamic arrays resized by setting their length property - the memory is filled with 0 instead of the default value. So floating point values are filled with 0 instead of NaN and strings are filled with 0 instead of 0xFF. If you allocate a dynamic array using "new" it will get the right default values.

Thanks, Ben. For a while we should be careful because if it will be fixed it could change behavior of existing code. So it is better not to use default values for a while. Andrew.
Apr 06 2005
prev sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 6 Apr 2005 16:16:58 -0700, Andrew Fedoniouk  
<news terrainformatica.com> wrote:
 In D I can declare new POD type with default value
 like

 typedef uint symbol_t = 0xFFFFFFFF;

 This is just perfect and almost works.
 But not here:

 symbol_t[char[]] symbols;
 symbol_t v = symbols["arg"];

 In ideal world 'v' should have 0xFFFFFFFF value
 but it has 0 instead. Is this so by design?

It seems to suffer from the same thing as a dynamic array, that is, if you create the type in-advertantly or indirectly it is initialised to 0, not it's init value. eg: import std.stdio; typedef uint symbol_t = 0xffffffff; void main() { writefln("[symbol_t]"); { symbol_t[char[]] symbols; symbol_t v = symbols["arg"]; symbol_t[1] test; symbol_t[] test2; symbol_t test3; test2.length = 1; writefln("%x",v); writefln("%x",test[0]); writefln("%x",test2[0]); writefln("%x",test3); } writefln(""); writefln("[CHAR]"); { char[char[]] symbols; char v = symbols["arg"]; char[1] test; char[] test2; char test3; test2.length = 1; writefln("%x",v); writefln("%x",test[0]); writefln("%x",test2[0]); writefln("%x",test3); } } Output: [symbol_t] 0 ffffffff 0 ffffffff [CHAR] 0 ff 0 ff The question is, is this a good thing? or a bad thing? Regan
Apr 07 2005
parent "Walter" <newshound digitalmars.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsouwe01t23k2f5 nrage.netwin.co.nz...
 The question is, is this a good thing? or a bad thing?

It's a bad thing. It should be initialized to the .init value.
Apr 15 2005