kotools / types

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

✨ New `StrictlyNegativeInt.Companion.create` function with custom message #598

Closed LVMVRQUXL closed 7 months ago

LVMVRQUXL commented 8 months ago

πŸ“ Description

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

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

This function throws an IllegalArgumentException with the specified message if the specified number is greater 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(StrictlyNegativeInt.create(-1) { "$it should be less than zero." }) // -1
StrictlyNegativeInt.create(0) { "$it should be less than zero." } // throws an exception with the specified message
StrictlyNegativeInt.create(-1) { " " } // throws an exception with a generic message

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

βœ… Checklist