Skip to content

Instantly share code, notes, and snippets.

@majk-p
Created July 14, 2022 19:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
unsafeorderline.sc
//> using scala "2"
case class UnsafeOrderLine(product: String, quantity: Int)
object UnsafeOrderLine {
def safeApply(product: String, quantity: Int): UnsafeOrderLine = {
if (product.isEmpty())
throw new RuntimeException("Product is empty")
else if (quantity <= 0)
throw new RuntimeException("Quantity lower than 1")
else
UnsafeOrderLine(product, quantity)
}
}
// Works fine!
println(UnsafeOrderLine.safeApply("123", 10))
// Throws runtime exception 👇
UnsafeOrderLine.safeApply("", 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment