Reputation: 983
If I am returning a Seq[T] from a function, when there maybe a chance that it is empty, is it still a Seq or will it error?
In other words, do I need to wrap it in an Option or is that overkill?
Upvotes: 1
Views: 2527
Reputation: 40461
It's generally overkill although it may convey some information depending on the context. Suppose you have a huge database of people, where some data could be missing. You could write queries like:
def getChildren( p: Person ): Seq[Person]
But if it returns an empty sequence, you cannot guess if the data is missing or if the data is available that there are no children. In contrast with the definition:
def getChildren( p: Person ): Option[Seq[Person]]
You will obtain None
when the data is missing and Some(s)
where s
is an empty sequence if there are no children.
Upvotes: 4