can anyone think of a more idiomatic way to do thi...
# announcements
k
can anyone think of a more idiomatic way to do this?
Copy code
if (error != null) throw error
c
not much different, but you can do
error?.let { throw it }
1
😄 1
2
j
It is already idiomatic. Anything else you do will be either trying to be clever or code golfing character count. Or both.
👍 2
k
Good point. I think it's just the mechanics of
throw
that make this feel more awkward than other, similar code. Parameters that are exceptions are often nullable. I almost wish that
throw null
did nothing or
error?.throw
worked. I'm fine writing the lengthier version, I just had a suspicion that there might be some other stdlib approach out there that I didn't know about.
a
You can always define your own extension function. But again, this is already ideomatic.
d
If
throw null
worked, then everywhere a
throw e
occured, I would have to double-check that
e
is non-null, or else it isn't a guaranteed throw and the remaining code in that block might still happen.
j
throw null
works in Java and in Java bytecode as an easy way to throw an NPE
👍 1
🔥 1
e
but harder to figure out why he wrote so