NumberTypeTester

Classifies platform Numbers (e.g. Int, Double, Float, or any other JVM/JS numeric boxed type encountered while converting external, non-Prolog values into Prolog terms) as integral or decimal, and converts them into the arbitrary-precision BigInteger/BigDecimal types used internally to represent Prolog numbers, based on their String representation rather than on their runtime class.

Classification looks at Number.toString (memoized per-instance in an internal Cache, since the same number is typically inspected more than once when it is turned into a term) instead of switching on the concrete Number subtype, which keeps this tester agnostic to whatever numeric types a given host platform (JVM, JS, ...) or interop layer (e.g. Java/Python object bridging in the oop-lib/dsl-core modules) happens to expose. Since isInteger, toInteger and toDecimal are declared as extensions on Number, they must be used from a receiver scope, typically via with:

private val numberTypeTester = NumberTypeTester()
// ...
with(numberTypeTester) {
when {
source.isInteger -> Integer.of(source.toInteger())
else -> Real.of(source.toDecimal())
}
}

The numberIsInteger, numberToInteger and numberToDecimal methods below offer the very same behaviour as plain (non-extension) methods, for callers (e.g. from Java, or other languages with no extension-function syntax) that cannot conveniently open a with block.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Whether this number's String representation (as per Number.toString) consists exclusively of decimal digits, i.e. it has no sign, decimal point, or exponent part.

Functions

Link copied to clipboard

Non-extension equivalent of Number.isInteger, for callers that cannot use a with receiver scope.

Link copied to clipboard
fun numberToDecimal(number: Number): BigDecimal

Non-extension equivalent of Number.toDecimal, for callers that cannot use a with receiver scope.

Link copied to clipboard
fun numberToInteger(number: Number): BigInteger

Non-extension equivalent of Number.toInteger, for callers that cannot use a with receiver scope.

Link copied to clipboard
fun Number.toDecimal(): BigDecimal

Converts this number into an arbitrary-precision BigDecimal, by parsing its String representation.

Link copied to clipboard
fun Number.toInteger(): BigInteger

Converts this number into an arbitrary-precision BigInteger, by parsing its String representation. Should only be called when isInteger is true.