AA default value?

Janice Caron caron800 at googlemail.com
Fri Jan 25 06:58:16 PST 2008


On 1/25/08, Ary Borenszweig <ary at esperanto.org.ar> wrote:
> Janice Caron wrote:
> > Wouldn't
> > it be nicer if, instead, it simply returned
> >
> >       typeof(aa[key]).init
> >
> > if the key was not present in the array?
>
> But then you can't tell wheter the key was present and the value was
> init, or it was not present at all.

If knowing that is important, you must use "in", as you always have
done. I wasn't for one moment suggesting deprecating "in". However,
there are plenty of circumstances where the distinction is
unimportant.

Another approach would be to specify the default at the time you make
the call (instead of relying on init). Of course, for that you'd need
a new syntax - e.g.

    aa[key; defaultValue]
or
    aa[key ? defaultValue]
or
    aa[key else defaultValue]
etc.

But yeah - under the first idea, if you really need to tell whether
the key was present and the value was init, you'd just do

    key in init

i.e. no change. Existing code still does the job.



More information about the Digitalmars-d mailing list