kotools / types

Explicit types for Kotlin Multiplatform.
https://types.kotools.org
MIT License
90 stars 6 forks source link

✨ New `PositiveInt` type in `org.kotools.types` package #637

Closed LVMVRQUXL closed 5 months ago

LVMVRQUXL commented 7 months ago

📝 Description

We want to introduce a PositiveInt experimental type, in the org.kotools.types package of the types Gradle subproject, for representing an integer of type Int that is greater than zero. This type should be serializable as Int.

Here's the Application Programming Interface (API) goal for this type:

// In 'types' Gradle subproject:
interface PositiveInt {
    override fun equals(other: Any?): Boolean
    override fun hashCode(): Int
    fun toInt(): Int
    override fun toString(): String

    companion object {
        val min: PositiveInt
        val max: PositiveInt

        fun fromInt(number: Int): PositiveInt = TODO()
        fun fromIntOrNull(number: Int): PositiveInt? = TODO()

        fun random(): PositiveInt
    }
}

// In 'types-kotlinx-serialization' Gradle subproject:
val KotoolsTypesSerializers.positiveInt: SerializersModule

🔗 Dependencies

This issue is blocked by the following ones:

✅ Checklist

LVMVRQUXL commented 7 months ago

In the future, we could make instances of this type comparable with themselves and the integer types from the Kotlin standard library.

interface PositiveInt : Comparable<PositiveInteger> {
    operator fun compareTo(other: Byte): Int
    operator fun compareTo(other: Short): Int
    operator fun compareTo(other: Int): Int
    operator fun compareTo(other: Long): Int
    operator fun compareTo(other: PositiveInteger): Int
}
LVMVRQUXL commented 7 months ago

In the future, we could add support for basic arithmetic operations +, -, *, / and %.

LVMVRQUXL commented 7 months ago

In the future, we could provide additional conversions for the PositiveInt.

interface PositiveInt {
    fun toByte(): Byte
    fun toShort(): Short
    fun toLong(): Long
    fun toFloat(): Float
    fun toDouble(): Double
}
LVMVRQUXL commented 6 months ago

In the future, we could introduce types representing the digits 1, 2, 3, 4, 5, 6, 7, 8 and 9. The digit 0 is already represented by the Zero experimental type in the org.kotools.types package.

It may also be interesting to move types related to digits in a new org.kotools.types.digit package.

LVMVRQUXL commented 5 months ago

This type will be implemented internally for the PositiveInteger type (see issue #661).