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