API guidelines #60
Replies: 5 comments 1 reply
-
Member and extension functions
Like mentioned in the Kotlin guidelines, we should write only the core of the API as member functions and everything else as extension functions. For example, the /** Representation of all integers. */
sealed interface AnyInt {
/** Returns this integer as an [Int]. */
fun toInt(): Int
/** Returns the string representation of this integer. */
override fun toString(): String
} As we can see in this definition of This design choice is linked with our Less is more principle in our design goals. |
Beta Was this translation helpful? Give feedback.
-
Inputs validationAs recommended by the Kotlin team, we should use the sealed interface NonZeroInt
/**
* Returns this number as an encapsulated [NonZeroInt], which may involve rounding or truncation,
* or returns an encapsulated [IllegalArgumentException] if this number equals zero.
*/
fun Number.toNonZeroInt(): Result<NonZeroInt> = runCatching {
require(toInt() != 0) { "Number shouldn't be 0." }
TODO("Build a NonZeroInt instance.")
} So, we should make this validation as early as possible, so users don't need to dive into complex stack traces. |
Beta Was this translation helpful? Give feedback.
-
Avoiding variable number of argumentsUsing variable number of arguments copies arrays under the hood. For instance:
|
Beta Was this translation helpful? Give feedback.
-
Avoid data classes in public APIIn Kotools Types 4, some types are defined as data classes ( Instead, we should define the API with classes or sealed interfaces for enabling backward compatibility. |
Beta Was this translation helpful? Give feedback.
-
Avoiding
|
Beta Was this translation helpful? Give feedback.
-
💡 The idea
We would like to follow the official Kotlin guidelines for libraries.
Here's the list of points discussed for the next versions:
infix
keyword onoperator
function🙏 Help needed
Please give this post a reaction if you are interested in this next version, or comment it below if you have any suggestion.
We would love to have your feedback!
Beta Was this translation helpful? Give feedback.
All reactions