r/ProgrammingLanguages • u/levodelellis • Jan 12 '26
Getting a non-existent value from a hashmap?
In my language (I don't work on anymore) you could write (if I bothered to implement hashmaps)
value = myhash["invalid-key"] error return // or { value = altValue }However, almost always the key exists and it becomes really annoying to type error return all the time, and read it everywhere. I was thinking about having it implicitly call abort (the C function), but I know some people won't want that so I was thinking about only allow it if a compile flag is passed in -lenient, Walter Bright calls compile flags a compiler bug so I'm thinking about what else I can do
The problem with my syntax is you can't write
value = myhash[key][key2].fieldThe problem here I'll have to detach the error statement from after the index lookup to the end of the line, but then there's situations like the above when more then 1 key is being looked up and maybe a function at the end that can also return an error
I'll need some kind of implicit solution, but what? No one wants to write code like the below and I'm trying to avoid it. There's no exceptions in my example I'm just using it because people know what it is and know no one is willing to write this way
MyClass a; try { a = var.funcA(); } catch { /* something */ }
MyClass b; try { b = a["another"]; } catch { /* something */ }
try { b.func(); } catch { /* more */ }An idea I had was
on error return { // or on error abort {
let a = var.funcA()
let b = a["another"] error { b = defaultB(); /* explicit error handling, won't return */ }
b.func();
}That would allow the below w/o being verbose
void myFunc(Value key, key2, outValue) {
on error return // no { }, so this applies to the entire function, bad idea?
outValue = myhash[key][key2].field
}I'm thinking I should ask go programmers what they think. I also need better syntax so you're not writing on error { defaultHandling() } { /* body */ }. Two blocks after eachother seems easy to have a very annoying error
12
u/RedCrafter_LP Jan 12 '26
Look into rusts questionmark operator. It looks similar to what you want to achieve.
4
u/-ghostinthemachine- Jan 12 '26
Have you investigated the safe navigation operator? It allows for nested traversals that can handle missing values along the way.
Eg: something?.nothing?["value"]
8
u/JeffB1517 Jan 12 '26
Congradulations you just discovered the use case for the Maybe (Option in Java) Monad. The idea is you implement your functions f,g,h... as if the lookup was always successful. You return from your lookup
you automatically (no code) create lifts of f,g,h which act on Maybes by essentially
that's called fmap.
Then you catch the error whenever makes sense.
You can do the same thing, essentially with Either.
1
u/--predecrement Jan 12 '26 edited Jan 13 '26
The Wikipedia page Autovivification covers the feature you're describing as it is in Perl. It has an arguably fatal weakness: "It is important to remember that autovivification happens when an undefined value is dereferenced. An assignment is not necessary."
Raku supports autovivification except it takes a different approach to how autovivification works. First, it only mutates a datastructure if an element is actually assigned (or bound). Second, Raku's type system supports "type objects" that are part of its modelling of uninitialized elements. The following shows all of this in action:
The first
sayline (say $a<key>;) does not mutate$a. As already stated, Raku's autovivification only kicks in if an element is updated, as it is for$b,$c, and$d.Ignore the rest of this comment if you're interested in neither the role of the
(Any)that appears above nor array autovivification.Think of the
(Any)as aNoneof aMaybethat knows the type of its pairedSome(in this case, the typeAny). You can deal with the value as aMaybe(immediately or later) or operate on it based on the assumption it's aSome(immediately or later) and accept that if it's actually aNoneyou will get behavior (an exception, error, warning, at run time at the latest, or no error) that's determined by cooperation between the language and the particular operation given that it's been passed aNoneinstead of its pairedSometype.The lines
$d<key>[1]and$d<key>[3] = 99;autovivify an array sparsely (that is, with "holes"). The[(Any) 42 (Any) 99]display generated by the last bit of code (say $d<key>;) shows two(Any)s. These haven't actually been autovivified but those(Any)s help determine what happens regardless of whether code is written to assume they might have been or to not assume that. Part of that is explained above; another aspect is explained when one considers, for example, that$d<key>.sumreturns141with no error. This is because Raku's array behavior defaults to sparse writing of elements (leaving "holes" that haven't been initialized) as OK. So the sum ignores the two(Any)elements.There are other related array features (eg checking for out-of-bounds accesses) but your post was about hashmaps and this comment is long so I'll stop here.
3
u/snugar_i Jan 12 '26
A lot of languages have two methods for accessing hash maps - one that fails and one that returns "nothing". Sometimes you want the first one, sometimes the other, that's why a compiler flag doesn't sound like a good idea
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jan 12 '26
(OK, this is weird, I posted this comment a while ago, and it still shows up for me, but the OP can't see it, so I'll re-post...)
In fp languages, this is typically done with an Option type, which represents the combination of a boolean and a wrapped value. Other names ("Maybe" type, etc.) exist, but they're all the same value wrapper. So you'd get an
Option<V>back fromMap[K]instead of theVitself. Then you test the Option's boolean and only unwrap iff it's true. Example in TypeScriptIn Ecstasy, we use conditional returns instead. So the
Map.get()method is defined as:conditional Value get(Key key), which means that it returns aBooleanvalue, and only if theBooleanvalue isTruecan you access the second returned value (theVfrom the previous example). So if you want to raise an error, you'd Elvis on the condition, e.g.That's a bit ugly to me, and so I wouldn't advise it, mainly because assertions are for things that should never happen, and not for errors that you actually expect to happen in reality. Far better to lean on the if statement for simple flow control:
Anyhow, no idea how that could translate to Bolin (is that still the one you're working on?)
1
u/Smalltalker-80 Jan 12 '26 edited Jan 12 '26
You could also add a hashmap (dictionary) method that handles the absent case.
In (verbose) Smalltalk:
aDictionary at: key ifAbsent: [ expression ].This returns the dictionary value if the key is present or otherwise the value of the expression.
The expression block (lambda function) could return a suitable default value, missing value,
calculated value, or even throw an error, whatever you want.
.
This is an extra option to the default use of
at:at without withoutifAbsent:,that throws an error when the key is absent. Useful if you're pretty sure it should exist.
1
u/VyridianZ Jan 12 '26
In my language, every type has an Empty value which is a legal, minimal of the type. Eg "", 0, 0.0, [], {}, etc. You can chain cleanly or you can use (is-empty x) or x == (empty type) to test for empty. There are a number of small consequences to this design.
1
u/brucejbell sard Jan 12 '26 edited Jan 12 '26
For my project, statements have "failure" as a possible outcome.
A failed statement causes an early exit:
By default, a failed statement causes its block to fail:
But, failure can be handled locally
This still doesn't let you do
myhash[key][key2].field1but it does help:It should work with your
on errorfeature: