Skip to content

Instantly share code, notes, and snippets.

View ex17_smart.scala
scala> new EmailAddress("@example.com") {}
<console>:15: error: illegal inheritance from sealed class EmailAddress
new EmailAddress(""){}
View ex16_smart.scala
scala> class EvilEmailAddress(address: String) extends EmailAddress(address)
<console>:14: error: illegal inheritance from sealed class EmailAddress
class EvilEmailAddress(address: String) extends EmailAddress(address)
View ex14_smart.scala
class EvilEmailAddress(address: String) extends EmailAddress(address)
EmailClient.sendEmail(from = new EvilEmailAddress("luis@example.com"), to = new EvilEmailAddress("@example.com")) {
...
}
View ex12_smart.scala
scala> EmailAddress("luis@example.com").map(_.copy(emailAddress = "@example.com"))
<console>:13: error: value copy is not a member of EmailAddress
EmailAddress("luis@example.com").map(_.copy(emailAddress = "@example.com"))
^
View ex10_smart.scala
scala> EmailAddress("luis@example.com").map(_.copy(emailAddress = "@example.com"))
res0: scala.util.Either[String,Either[String,EmailAddress]] = Right(Left(Invalid email address))
scala> for {
| e1 <- EmailAddress("luis@example.com")
| e2 <- e1.copy(emailAddress = "@example.com")
| } yield e2
res1: scala.util.Either[String,EmailAddress] = Left(Invalid email address)
View ex9_smart.scala
case class EmailAddress(emailAddress: String) {
def copy(emailAddress: String = emailAddress): Either[String, EmailAddress] = {
EmailAddress(emailAddress)
}
}
View ex8_smart.scala
scala> val emailAddress = EmailAddress("luis@example.com")
emailAddress: Either[String,EmailAddress] = Right(EmailAddress(luis@example.com))
scala> emailAddress.map(_.copy(emailAddress = "@example.com"))
res0: scala.util.Either[String,EmailAddress] = Right(EmailAddress(@example.com))