Skip to content

scala-hamsters/orEmpty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Default values for options (orEmpty)

This is a port of Hamsters orEmpty to Scala 2.13.

Default values for options avoid repeating the same getOrElse code again and again :

import io.github.hamsters.EmptyOptionValues._

val noneString: Option[String] = None
noneString.orEmpty  // ""

val noneInt: Option[Int] = None
noneInt.orEmpty // 0 

val noneList: Option[List[String]] = None
noneList.orEmpty  // List[String]()

To support other types, you have to declare a new implicit monoid for this types.

Example with the BigDecimal type (already supported) :

implicit val bigDecimalMonoid: Monoid[BigDecimal] = new Monoid[BigDecimal] {
 // method used by orEmpty
 override def empty: BigDecimal = BigDecimal(0)
 
 override def compose(l: BigDecimal, r: BigDecimal): BigDecimal = l + r
}

Install

Add this to you build.sbt :

resolvers += Resolver.bintrayRepo("loicdescotte", "Hamsters") 
libraryDependencies += "io.github.scala-hamsters" % "orEmpty" % "1.0.0"

About

Default values for options

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages