Skip to content

Instantly share code, notes, and snippets.

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