Idiomatic way to handle nested nullable objects in scala? -
Idiomatic way to handle nested nullable objects in scala? -
i'm working scala , inherited java code needs grafted in , unfortunately rewriting in scala isn't in cards. has nested object structure, , level can null. care values deep within nesting.
ideally, i'd this:
option(foo.blah.blarg.doh)
but if of foo.blah.blarg null, generate npe.
for i've taken wrapping in try:
try(option(foo.blah.blarg.doh)).getorelse(none)
note using .tooption doesn't work quite right can lead some(null) if final bit of chain null.
i'm not particularly fond of construct, other ideas out there?
take advantage of by-name parameters build own construct:
def handlenull[t](x: => t): option[t] = seek option(x) grab { case _: nullpointerexception => none } handlenull(foo.blah.blarg.doh)
scala
Comments
Post a Comment