scala - Elegant Way to Check toInt or toBoolean -
I have a group of fields that I am parsing in XML and they are This is how the code looks without the handle. Parental node is always present but can not have hair nodes or fields. You can use From here you have several options to deal with it. I guess That, in the case where the area does not exist, you have no default value 1 failure is not important, then you can use directly in 2 Pattern-Mailing It should be used only when you have many ways of calling and / or exception handling. int or type on the basis of
boolean field The problem is that in some cases of the XML file this tag may not exist, it naturally throws a runtime error when I have a blank for
boolean or
int Try to convert code> string . I'm new to Scala, is there a way to use the
option class to make it better?
(parentNode / childNote / area) .text.toBoolean
scala.util.Try for it. If code inside
try -block is successful, it will give a
success (result) . Otherwise a
failure (exception) has been refunded. Try
(parentNode / childNote / field) .text.toBoolean)
getOrElse and in a failure case default value will be used. Try
(parentNode / childNote / area) .text.toBoolean) .getOrElse (DefaultValue)
(parentNode / childNote / field) .text.toBoolean) match {case success (bull) = & gt; {// calculations} case failure (former) = & gt; {// do something with exception}}
Comments
Post a Comment