July 28, 2005 AA indexing behavior | ||||
---|---|---|---|---|
| ||||
As an experiment I've changed the MinTL AA indexing behavior for HashAA and SortedAA to give more flexibility: 1) opIndex returns a settable missing value (the property is called 'missing'). The default missing value is Value.init. The Value take(Key) function also returns the default missing value on failure. 2) added bool contains(Key key) and bool contains(Key key, out Value value) 3) added Value* get(Key key, int action = NullOnMiss) where action can be one of the three predefined enum values NullOnMiss, ThrowOnMiss and InsertOnMiss So the old opIn (equivalent to the builtin 'in') is now get(key). Most of the time, though, the indexing or contains functions will do. For instance names[phone] = a_name; Times when a get() is needed would be for fancy things like (*count.get(word,InsertOnMiss))++ which is how one can mimic the builtin lvalue indexing behavior. One interesting question is if the get(word,InsertOnMiss) should insert the missing value or Value.init. Right now I have it inserting Value.init. Anyhow, comments welcome. -Ben |
July 28, 2005 Re: AA indexing behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | > 3) added Value* get(Key key, int action = NullOnMiss) where action can be
> one of
> the three predefined enum values NullOnMiss, ThrowOnMiss and InsertOnMiss
slight modification: instead of the action enum I'll make get take an optional bool to distinguish null vs throw and I'll add a new function put(key) that inserts on missing. That way get(key) never modifies the array and the enum can be removed.
|
Copyright © 1999-2021 by the D Language Foundation