Replies: 2 comments
-
Another approach would be to provide only factory functions prefixed with fun PositiveInt.Companion.fromNumber(number: Number): PositiveInt = TODO()
fun PositiveInt.Companion.fromNumberOrNull(number: Number): PositiveInt? = TODO()
fun PositiveInt.Companion.fromString(value: Any): PositiveInt = TODO()
fun PositiveInt.Companion.fromStringOrNull(value: Any): PositiveInt? = TODO() Here's an example of calling these functions from Kotlin code: PositiveInt.fromNumber(123)
PositiveInt.fromNumberOrNull(-1.5) // returns null
PositiveInt.fromString("123")
PositiveInt.fromStringOrNull("oops") // returns null For improving the user experience from Java code, we should also omit the PositiveInt.fromNumber(123);
PositiveInt.fromNumberOrNull(-1.5); // returns null
PositiveInt.fromString("123");
PositiveInt.fromStringOrNull("oops"); // returns null |
Beta Was this translation helpful? Give feedback.
-
For improving the design for Kotlin and Java users, we could simply provide constructors for Kotlin and Java and fun PositiveInt(number: Int): PositiveInt
@JvmSynthetic // makes it unavailable from Java sources
fun PositiveInt.Companion.orNull(number: Int): PositiveInt? |
Beta Was this translation helpful? Give feedback.
-
Our current API providing a heavy usage of the Result type and extension functions for creating types, we would like to improve the design by providing constructors, constructor-like functions for interfaces, and
orNull
factory functions on companion object instead.Here's an example of creating a
PositiveInt
:The
orNull
function being replaceable by using therunCatching
function, we are also considering only providing constructors and constructor-like functions.Beta Was this translation helpful? Give feedback.
All reactions