Retrieving Head of an Option[List[...]]
Given a Some(List("hello")) and wanting to get "hello", I observed the
following behavior.
scala> val list = Some(List("hello"))
list: Some[List[String]] = Some(List(hello))
scala> list.head.head
res3: String = hello
Then, I checked, the Scaladocs and saw that head will Select the first
element of this iterable collection. It also points out that an exception
will be thrown if the collection is empty.
Is this retrieval of the first element of a Option[List] considered
idiomatic?
No comments:
Post a Comment