Skip to content

Instantly share code, notes, and snippets.

@majk-p
Created July 14, 2022 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majk-p/4845b8efadaab3c069795d0a587a77f3 to your computer and use it in GitHub Desktop.
Save majk-p/4845b8efadaab3c069795d0a587a77f3 to your computer and use it in GitHub Desktop.
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