www.digitalmars.com         C & C++   DMDScript  

D - assocative array defaults?

reply Patrick Down <pat codemoon.com> writes:
I'm a little surprised that this does not throw
an error.  It prints a = 0.  When an assocative array
doesn't have a key does it return the type.init
value?

int main(char[][] args)
{
  int[char[]] intmap;
  
  int a = intmap["betty"];
  
  printf("a = %d\n",a);

  return 0;
}
Jul 31 2002
next sibling parent Burton Radons <loth users.sourceforge.net> writes:
Patrick Down wrote:

 I'm a little surprised that this does not throw
 an error.  It prints a = 0.  When an assocative array
 doesn't have a key does it return the type.init
 value?
Yup. You can use the "in" operator to make the check yourself. I see the reasons as being: - Exceptions are far more expensive than normal execution, so exception-dependent operations are avoided. - Most language-based exceptions should be turned off when all debugging is killed, but this would alter normal execution here. - It's often convenient. If you're treating it as a set, for example, then just normal testing tells you whether an object is included in it. For objects, you can get the in and the what at the same time without having to build a try/catch around it. But it should be noted in the standard.
 int main(char[][] args)
 {
   int[char[]] intmap;
   
   int a = intmap["betty"];
   
   printf("a = %d\n",a);
 
   return 0;
 }
 
Jul 31 2002
prev sibling parent Pavel Minayev <evilone omen.ru> writes:
On Thu, 1 Aug 2002 02:12:31 +0000 (UTC) Patrick Down <pat codemoon.com> wrote:

 
 I'm a little surprised that this does not throw
 an error.  It prints a = 0.  When an assocative array
 doesn't have a key does it return the type.init
 value?
In fact, it _does_ create a key and initializes it with default value. This is a typical behaviour for associative array in most languages, including std::map.
Jul 31 2002