This file contains hidden or 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
| fun main(args: Array<String>) { | |
| val contactResult: Result<ContactInfo> = ::ContactInfo.curry() | |
| .on(PersonalName.of("Foo", "Bar", "J.")) | |
| .on(Email.Unverified.of("[email protected]")) | |
| .result | |
| contactResult | |
| .map { contact -> | |
| when(contact.email){ | |
| is Email.Unverified -> EmailVerificationService.verify(contact.email) |
This file contains hidden or 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
| val contact: Result<ContactInfo> = ::ContactInfo.curry() | |
| .on(PersonalName.of("Foo", "Bar", "J.")) | |
| .on(Email.Unverified.of("[email protected]")) | |
| .result |
This file contains hidden or 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 PasswordResetService(){ | |
| fun send(email: Email.Verified): Unit = TODO("send reset") | |
| } | |
| class EmailVerificationService(){ | |
| fun verify(unverifiedEmail: Email.Unverified): Email.Verified? = | |
| if(incredibleConditions())Email.Verified(unverifiedEmail) else null | |
| private fun incredibleConditions() = true | |
| } |
This file contains hidden or 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
| data class ContactInfo( | |
| val name: PersonalName, | |
| val email: Email) | |
| data class PersonalName( | |
| val firstname: NotEmptyString, | |
| val middleInitial: NotEmptyString?, | |
| val lastname: NotEmptyString){ | |
| ... // construction code here |
This file contains hidden or 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 class Email(val value: String){ | |
| data class Verified(private val email: Unverified): Email(email.value) | |
| class Unverified private constructor(value: String): Email(value){ | |
| companion object{ | |
| fun of(value: String): Result<Email.Unverified> = valikate { | |
| validate(Email.Unverified(value)){ | |
| validate(Email.Unverified::value).isEmail() | |
| } | |
| } | |
| } |
This file contains hidden or 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
| fun sendPasswordRecovery(email: Email.Verified) { sendTo(email.value) } |
This file contains hidden or 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
| fun sendPasswordRecovery(email: Email) { if(email.verified) sendTo(email.value) } |
This file contains hidden or 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
| data class Email( | |
| val value: String, | |
| val isVerified: Boolean) |
This file contains hidden or 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 class Email(open val value: String){ | |
| data class Verified(private val email: Unverified): Email(email.value) | |
| data class Unverified(override val value: String): Email(value) | |
| } |
This file contains hidden or 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
| data class PersonalName( | |
| val firstname: NotEmptyString, | |
| val middleInitial: NotEmptyString?, | |
| val lastname: NotEmptyString){ | |
| companion object { | |
| fun of(firstname: String, lastname: String, middleInitial: String? = null): Result<PersonalName> = | |
| ::PersonalName.curry() | |
| .on(NotEmptyString.of(firstname)) | |
| .on(middleInitial?.run { NotEmptyString.of(middleInitial) } ?: null.ok()) |
NewerOlder