Zyle Moore
04/17/2024, 9:28 PMvalue class NonEmptyString(private val raw: String) {
init {
require(raw.isNotEmpty())
}
}
value class Digits(private val raw: NonEmptyString) {
init {
require(raw.all(Char::isDigit))
}
}
val nes = NonEmptyString("asdf")
val digits = Digits("123")// doesn't work, but I want it to
val nesDigits = Digits(NonEmptyString("123"))// works, but feels clunky
ephemient
04/17/2024, 9:53 PMvalue class Digits(private val raw: NonEmptyString) {
constructor(raw: String) : this(NonEmptyString(raw))
Zyle Moore
04/17/2024, 9:55 PMZyle Moore
04/17/2024, 9:57 PMe: file///F/github/Zymus/ideas/libraries/vcard/vcard-model/src/commonMain/kotlin/games/studiohummingbird/vcard/primitives/Digits.kt167 Platform declaration clash: The following declarations have the same JVM signature (constructor-impl(Ljava/lang/String;)Ljava/lang/String;):
fun `constructor-impl`(raw: NonEmptyString): Digits defined in games.studiohummingbird.vcard.primitives.Digits
fun `constructor-impl`(s: String): Digits defined in games.studiohummingbird.vcard.primitives.Digits
ephemient
04/17/2024, 10:01 PMephemient
04/17/2024, 10:02 PMvalue class Digits(private val raw: NonEmptyString)
fun Digits(raw: String): Digits = Digits(NonEmptyString(raw))
Zyle Moore
04/17/2024, 10:05 PMpackage games.studiohummingbird.vcard.primitives
import arrow.core.Predicate
import kotlinx.serialization.Serializable
/**
* Not all Unicode Digits, just ASCII
*/
private val DIGIT_RANGE = '0'..'9'
fun NonEmptyString.allDigits() = all(DIGIT_RANGE::contains)
fun NonEmptyString.digits() = Digits(this)
@Serializable
@JvmInline
value class NonEmptyString(private val raw: String)
{
init {
require(raw.isNotEmpty()) { "must not be empty" }
}
fun all(match: Predicate<Char>): Boolean = raw.all(match)
}
@Serializable
@JvmInline
value class Digits(private val raw: NonEmptyString)
{
constructor(s: String) : this(NonEmptyString(s))
init {
require(raw.allDigits()) { "must contain only digit" }
}
}
Zyle Moore
04/17/2024, 10:06 PMZyle Moore
04/17/2024, 10:08 PMephemient
04/17/2024, 10:09 PMZyle Moore
04/17/2024, 10:11 PMZyle Moore
04/17/2024, 10:32 PMZyle Moore
04/17/2024, 10:34 PMoperator fun invoke
on the companion object
Adam S
04/18/2024, 7:53 AM