简体   繁体   中英

How to pretty print value returned by Option in scala?

I have a function which returns an "Option[myDataStructure]. When I call this from Zeppelin notebook, it says that it cannot call "show()" on the value as it of type "option". Is there any other way using which I can pretty print my returned value as a dataset? Printing using println is really clumsy.

val returnValue: Option[myDataStructure] = myFunction(a,b)
returnValue.show(10,false)

You can use foreach to perform side-effecting action on Option :

returnValue.foreach(_.show(10,false))

It will only run if the Option is Some and therefore it's safer than calling get which may throw java.util.NoSuchElementException in the case when Option is None .

您可以通过使用get获得Option中的值

returnValue.get.show(10,false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM