Created
July 14, 2022 19:09
-
-
Save majk-p/4845b8efadaab3c069795d0a587a77f3 to your computer and use it in GitHub Desktop.
unsafeorderline.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> 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