kotools / types

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

✨ New `StrictlyPositiveInt.Companion.create` function with custom message #597

Closed LVMVRQUXL closed 7 months ago

LVMVRQUXL commented 8 months ago

📝 Description

We would like to introduce an experimental StrictlyPositiveInt.Companion.create for accepting a customized exception message.

fun StrictlyPositiveInt.Companion.create(
    number: Number,
    message: (Number) -> Any
): StrictlyPositiveInt

This function throws an IllegalArgumentException with the specified message if the specified number is less than or equals zero. If the message has a blank string representation, it throws the exception with a generic message instead.

Here are some examples of calling this function from Kotlin code:

println(StrictlyPositiveInt.create(1) { "$it should be greater than zero." }) // 1
StrictlyPositiveInt.create(0) { "$it should be greater than zero." } // throws an exception with the specified message
StrictlyPositiveInt.create(0) { " " } // throws an exception with a generic message

The StrictlyPositiveInt type being an inline value class, this function shouldn't be available yet for Java users.

✅ Checklist

LVMVRQUXL commented 7 months ago

Another implementation of this type will be available after resolving #637.