I think Associative Array should throw Exception

Steven Schveighoffer schveiguy at gmail.com
Tue Sep 1 18:55:20 UTC 2020


On 9/1/20 2:20 PM, Jesse Phillips wrote:

> Using RangeError is nice as it allows code to use array index inside 
> `nothrow.`

This is the big sticking point -- code that is nothrow would no longer 
be able to use AAs. It makes the idea, unfortunately, a non-starter.

What is wrong with using `in`? I use this mostly:

if(auto v = key in aa) { /* use v */ }

Note, that in certain cases, I want to turn *normal* array access errors 
into exceptions, because I always want bounds checking on, but I don't 
want a (caught) programming error to bring down my whole vibe.d server.

So I created a simple wrapper around arrays which throws exceptions on 
out-of-bounds access. You could do a similar thing with AAs. It's just 
that the declaration syntax isn't as nice.

-Steve


More information about the Digitalmars-d-learn mailing list