Skip to content

Instantly share code, notes, and snippets.

import io.konad.Result
inline class NotEmptyString
@Deprecated(level = DeprecationLevel.ERROR, message = "use companion method 'of'")
constructor(val value: String){
companion object{
@Suppress("DEPRECATION_ERROR")
fun of(value: String): Result<NotEmptyString> = valikate {
validate(NotEmptyString(value)){
validate(NotEmptyString::value).isNotBlank()
inline class NotEmptyString
@Deprecated(level = DeprecationLevel.ERROR, message = "use companion method 'of'")
constructor(val value: String){
@Suppress("DEPRECATION_ERROR")
companion object{
fun of(value: String): NotEmptyString = validate(NotEmptyString(value)){
validate(NotEmptyString::value).isNotBlank()
}
}
}
data class PersonalName(
val firstname: NotEmptyString,
val middleInitial: NotEmptyString?, // Here it clearly states that could be missing
val lastname: NotEmptyString)
data class PersonalName(
val firstname: String,
val middleInitial: String,
val lastname: String)
data class Email(
val value: String,
val isVerified: Boolean)
data class ContactInfo(
data class ContactInfo(
val firstname: String,
val middleInitial: String,
val lastname: String,
val email: String,
val emailIsVerified: Boolean)
fun becomeRich(crypto: CryptoInfo, buyInfo: BuyCryptoInfo): Boolean = false
val user = CryptoUser(username = "foo.bar", ..., ...)
val crypto = CryptoInfo("Bitcoin")
val youGotRich1: Result<Boolean> = ::becomeRich + crypto + BuyCryptoInfo.from1(user)
// or
val youGotRich2: Result<Boolean> = BuyCryptoInfo.from1(user)
import io.konad.*
data class BuyCryptoInfo(
val username: String,
val phoneNumber: PhoneNumber,
val creditCard: CreditCard,
val kycVerification: KycVerificationData){
companion object{
// If you want to go for errors
data class BuyCryptoInfo(
val username: String,
val phoneNumber: PhoneNumber,
val creditCard: CreditCard,
val kycVerification: KycVerificationData){
companion object{
fun from1(user: CryptoUser): Result<BuyCryptoInfo> = with(user){
val listOfErrors = mutableListOf<String>()
sealed class Result<out T>{
data class Ok<out T>(val value: T): Result<T>()
data class Error(val description: String): Result<Nothing>()
}
data class BuyCryptoInfo(
val username: String,
val phoneNumber: PhoneNumber,
val creditCard: CreditCard,
val kycVerification: KycVerificationData){
data class BuyCryptoInfo(
val username: String,
val phoneNumber: PhoneNumber,
val creditCard: CreditCard,
val kycVerification: KycVerificationData){
companion object{
fun from1(user: CryptoUser): BuyCryptoInfo? = with(user){
if(phoneNumber != null && creditCard != null && kycVerification != null)
BuyCryptoInfo(username, phoneNumber, creditCard, kycVerification)