Thread overview | |||||
---|---|---|---|---|---|
|
August 01, 2002 assocative array defaults? | ||||
---|---|---|---|---|
| ||||
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; } |
August 01, 2002 Re: assocative array defaults? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | 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; > } > |
August 01, 2002 Re: assocative array defaults? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | 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.
|
Copyright © 1999-2021 by the D Language Foundation