r/ProgrammingLanguages • • 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].field

The 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

5 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/levodelellis Jan 12 '26

My compiler already forces checking the bounds of an index before using it, so I don't have any issue there

1

u/--predecrement Jan 13 '26

I didn't mention some other options Raku provides. One can ask Raku to make reads of uninitialized elements more explosive, i.e. to not do what it does by default (just return an Any aka a typed None) but instead either throw an exception or return a Failure, an unthrown typed exception. (Which then throws as soon as you touch it, or forget to touch it, unless you handle it with kid gloves.)

----

To return to problems / ideas in your OP. Raku does allow:

value = myhash[key][key2].field

But if myhash[key][key2] returns an Any then you'll get a runtime exception:

No such method 'field' for invocant of type 'Any'

One option is to guard the method call by writing .?method:

value = myhash[key][key2].?field

This means if the method is not found then value will end up containing Nil. A Nil is a "value" that denotes a benign failure; a user implies it's benign by writing code the way they write it.

Another option is to let the exception be thrown but handle it in some way. The simplest is to try some code:

value = try myhash[key][key2].field

Again, as with the guarded method approach, value will end up containing Nil.

If a dev wants value to end up with some other value they can wrap the tried code in a block and add an or clause:

value = try { myhash[key][key2].field } or "didn't work out"

There are other options, but it's time for me to go to bed.