ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
361 stars 41 forks source link

orNull way #283

Open slavonnet opened 7 months ago

slavonnet commented 7 months ago

Hello. I think that trows is not kotlin way

Please add orNull version to all toDecimals parsers. Look to some examples like String.toBooleranOrNull implementation

for example i create this

fun String.toBigDecimalOrNull (exponentModifier: Long? = null, decimalMode: DecimalMode? = null) : BigDecimal? = 
    runCatching { this.toBigDecimal(exponentModifier, decimalMode) }.getOrNull()

fun Byte.toBigDecimalOrNull (exponentModifier: Long? = null, decimalMode: DecimalMode? = null) : BigDecimal? =
    runCatching { this.toBigDecimal(exponentModifier, decimalMode) }.getOrNull()

fun Float.toBigDecimalOrNull (exponentModifier: Long? = null, decimalMode: DecimalMode? = null) : BigDecimal? =
    runCatching { this.toBigDecimal(exponentModifier, decimalMode) }.getOrNull()

fun Double.toBigDecimalOrNull (exponentModifier: Long? = null, decimalMode: DecimalMode? = null) : BigDecimal? =
    runCatching { this.toBigDecimal(exponentModifier, decimalMode) }.getOrNull()

fun  Long.toBigDecimalUsingSignificandAndExponentOrNull(exponent: Long, decimalMode: DecimalMode? = null) : BigDecimal? =
    runCatching { this.toBigDecimalUsingSignificandAndExponent(exponent, decimalMode) }.getOrNull()

fun  Int.toBigDecimalUsingSignificandAndExponentOrNull(exponent: Long, decimalMode: DecimalMode? = null) : BigDecimal? =
    runCatching { this.toBigDecimalUsingSignificandAndExponent(exponent, decimalMode) }.getOrNull()

P.S. Please add @Throws to throwable funcs. P.S.S @SharedImmutableDepricated

P.P.P.S Intrestiong info from kotlin. not for this issue. Types definitions variants:

Lysander commented 6 months ago

As I mentioned in Issue #253 then orElse and/or orDefault would also make sense.