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.
Properties
Functions
Non-extension equivalent of Number.isInteger, for callers that cannot use a with receiver scope.
Non-extension equivalent of Number.toDecimal, for callers that cannot use a with receiver scope.
Non-extension equivalent of Number.toInteger, for callers that cannot use a with receiver scope.