www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Rectangular or 2d associative arrays

reply David Freitas <jddcef gmail.com> writes:
Hello All!

Look at this small program and look at the output in the comments:
///////////////////////////////////
 import std.stdio;
 void main() 
 {
         int[char] x;         // Visually: = ['a':1, 'b':2] // associative array
         x['b'] = 123;
         writefln(x);  // this prints out: [b:2063597568]
         writefln(x['b']); // this prints out: 123
 }
/////////////////////////////////

This just doesn't seem intuitive to me? Why is there a "garbage" value being
printed out? 

Using a linux machine, with dmd 1.009.

Cheers!

David
Mar 16 2007
next sibling parent reply Falk Henrich <schreibmalwieder hammerfort.de> writes:
David Freitas wrote:

          int[char] x;         // Visually: = ['a':1, 'b':2] // associative
          array x['b'] = 123;
          writefln(x);  // this prints out: [b:2063597568]
          writefln(x['b']); // this prints out: 123
 
 This just doesn't seem intuitive to me? Why is there a "garbage" value
 being printed out?
What would you expect D to print out? How can the language know how to interpet arrays of some type? Hard coding some special output format for all types of arrays into the language is not a wise idea. Falk
Mar 16 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Falk Henrich wrote:
 David Freitas wrote:
 
          int[char] x;         // Visually: = ['a':1, 'b':2] // associative
          array x['b'] = 123;
          writefln(x);  // this prints out: [b:2063597568]
          writefln(x['b']); // this prints out: 123

 This just doesn't seem intuitive to me? Why is there a "garbage" value
 being printed out?
What would you expect D to print out? How can the language know how to interpet arrays of some type? Hard coding some special output format for all types of arrays into the language is not a wise idea.
Writef(ln) and other functions that do formatting use the TypeInfo provided by their varargs to construct readable representations of their parameters. This case just seems to be buggy.
Mar 16 2007
next sibling parent Falk Henrich <schreibmalwieder hammerfort.de> writes:
Frits van Bommel wrote:

 Falk Henrich wrote:
 David Freitas wrote:
 
          int[char] x;         // Visually: = ['a':1, 'b':2] //
          associative array x['b'] = 123;
          writefln(x);  // this prints out: [b:2063597568]
          writefln(x['b']); // this prints out: 123

 This just doesn't seem intuitive to me? Why is there a "garbage" value
 being printed out?
What would you expect D to print out? How can the language know how to interpet arrays of some type? Hard coding some special output format for all types of arrays into the language is not a wise idea.
Writef(ln) and other functions that do formatting use the TypeInfo provided by their varargs to construct readable representations of their parameters. This case just seems to be buggy.
I wasn't aware of that possibility (TypeInfo). Knowing this, the original question has more sense... Falk
Mar 16 2007
prev sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Frits van Bommel wrote:
 Falk Henrich wrote:
 David Freitas wrote:

          int[char] x;         // Visually: = ['a':1, 'b':2] // 
 associative
          array x['b'] = 123;
          writefln(x);  // this prints out: [b:2063597568]
          writefln(x['b']); // this prints out: 123

 This just doesn't seem intuitive to me? Why is there a "garbage" value
 being printed out?
What would you expect D to print out? How can the language know how to interpet arrays of some type? Hard coding some special output format for all types of arrays into the language is not a wise idea.
Writef(ln) and other functions that do formatting use the TypeInfo provided by their varargs to construct readable representations of their parameters. This case just seems to be buggy.
Probably related to bug 1000: http://d.puremagic.com/issues/show_bug.cgi?id=1000
Mar 16 2007
prev sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
David Freitas wrote:
 Hello All!
 
 Look at this small program and look at the output in the comments:
 ///////////////////////////////////
  import std.stdio;
  void main() 
  {
          int[char] x;         // Visually: = ['a':1, 'b':2] // associative
array
          x['b'] = 123;
          writefln(x);  // this prints out: [b:2063597568]
          writefln(x['b']); // this prints out: 123
  }
 /////////////////////////////////
 
 This just doesn't seem intuitive to me? Why is there a "garbage" value being
printed out? 
 
 Using a linux machine, with dmd 1.009.
Looks like it's (the value that gets inserted) << 24, or in other words the lower byte is put in the high byte, and the others are cleared. It looks like it's dependent on the key size: if the key type is changed to ushort the upper two bytes are filled with what should be the lower two bytes, and if it's a three-byte struct (with toString) the upper three bytes should be the lower three bytes and the lower byte seems to be garbage. This bug doesn't seem to affect GDC. It looks like gphobos' std.format.doFormat.formatArg.putAArray was patched to make sure access to AA internals is performed on the correct alignment boundary. This may have been done for portability reasons, but I'm guessing it also fixes this bug. Would anyone mind confirming this and submitting a bug + patch to bugzilla?
Mar 16 2007