foo.getA.getB.getC.getD.getE.getF.getG
Just use Option and for-comprehensions:
for {
f <- Option(foo)
a <- Option(f.getA)
b <- Option(a.getB)
c <- Option(b.getC)
} yield c
On Thu 07 Feb 2013 09:50:54 AM CET, Piotr Kopeć wrote:
Hi All,We have a java object *foo* and we want to work with it's deeply
I want to ask you about best practices in nested-null-from-java
handling :)
nested member *g
*
*foo*.getA.getB.getC.getD.getE.getF.getG
How would you access *g* if you know that each nested member can be
null and you don't want to explicitly declare reference and checking
for each nested member?
Thanks!
--
You received this message because you are subscribed to the Google
Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to scala-language+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-language+unsubscribe@googlegroups.com.
Try(foo.getA.getB.getC.getD.getE.getF.getG) recover { case _: NPE => something } toOption
Maybe best practice would be a macro, optionally(foo.getA.getB.getC.getD.getE.getF.getG).
Shouldn't "best practice" rather be to apply the tell don't ask principle? Or at least the law of demeter? Or does FP invalidate those guidelines?
BR
John
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
Now you try to use this code in tandem with Java code that--in Java
style--is using null to represent an optional value of its own. In
such a case, you really do want to distinguish between None and
Some(null). The former means the computation failed, and the latter
means that it succeeded but return null.
Why this way rather than None, SomeNull?