Use None or EmptyMyObj in scala?

I understand the concept. None I also understand how good it is to have MyObjas well MyEmptyObj, but I cannot understand when it is better to use Noneinstead MyEmptyObjand when I should use MyEmptyObjinstead None. In addition, I do not like to see methods that return Option[MyObj], it clutters up my code, which I prefer to return MyObj, and then easily calls its methods, such as MyObj.toJson, and therefore MyEmptyObj.toJsonwill know to represent itself, and not to have case None: return some empty jsonWhat do you think of this subject? On the other hand, I can say that it Nonegoes well with flatMap, etc., why choose?

when None

and when Empty?

+3
source share
1

None , , , .

, amountOwed, :

def amountOwed(bill: Int, alreadyPaid: Option[Int]): Option[Int] = {
  val owed = bill - alreadyPaid.getOrElse(0)
  if(owed == 0) None // nothing to pay!
  else Some(owed)
}

( ), , 0 , " " " ":

def amountOwed(bill: Int, alreadyPaid: Int): Int = {
  bill - alreadyPaid
}
+3

All Articles