0
# Mathematical Number Support
1
2
Abstract base classes and utilities for Scala's numeric type hierarchy, providing foundation for custom numeric types and mathematical operations. Includes runtime utilities, hash functions, and function interoperability support.
3
4
## Core Imports
5
6
```scala
7
import scala.math.ScalaNumber
8
import scala.runtime.Statics
9
import scala.runtime.function.{JProcedure0, JProcedure1, JProcedure2} // Scala 3 only
10
import scala.scalanative.runtime.BoxedUnit
11
```
12
13
## Capabilities
14
15
### Scala Number Base Class
16
17
Abstract base class for Scala's numeric type hierarchy.
18
19
```scala { .api }
20
abstract class ScalaNumber extends java.lang.Number {
21
protected def isWhole(): Boolean
22
def underlying(): Object
23
}
24
```
25
26
### Runtime Static Utilities
27
28
Core runtime utilities for hashing, memory operations, and error handling.
29
30
```scala { .api }
31
object Statics {
32
// Hash functions
33
def mix(hash: Int, data: Int): Int
34
def mixLast(hash: Int, data: Int): Int
35
def finalizeHash(hash: Int, length: Int): Int
36
def avalanche(h: Int): Int
37
def longHash(lv: Long): Int
38
def longHashShifted(lv: Long): Int
39
def doubleHash(dv: Double): Int
40
def floatHash(fv: Float): Int
41
def anyHash(x: Object): Int
42
43
// Utilities
44
def pfMarker: java.lang.Object
45
def releaseFence(): Unit
46
def ioobe[T](n: Int): T
47
}
48
```
49
50
### Function Interoperability (Scala 3)
51
52
Java-compatible procedure traits that return Unit but integrate with Java Function interfaces.
53
54
```scala { .api }
55
trait JProcedure0 extends scala.Function0[Object] with java.io.Serializable {
56
def applyVoid(): Unit
57
def apply(): Object
58
}
59
60
trait JProcedure1[T1] extends scala.Function1[T1, Object] with java.io.Serializable {
61
def applyVoid(t1: T1): Unit
62
def apply(t1: T1): Object
63
}
64
65
trait JProcedure2[T1, T2] extends scala.Function2[T1, T2, Object] with java.io.Serializable {
66
def applyVoid(t1: T1, t2: T2): Unit
67
def apply(t1: T1, t2: T2): Object
68
}
69
70
// ... Additional JProcedure traits from JProcedure3 to JProcedure22
71
```
72
73
### Trait Support
74
75
Marker class for trait initialization support.
76
77
```scala { .api }
78
class TraitSetter
79
```
80
81
### BoxedUnit Support
82
83
Scala Native-specific BoxedUnit implementation.
84
85
```scala { .api }
86
object BoxedUnit extends scala.runtime.BoxedUnit
87
```
88
89
## Usage Examples
90
91
```scala
92
import scala.math.ScalaNumber
93
import scala.runtime.Statics
94
95
// Extending ScalaNumber for custom numeric types
96
class MyNumber(value: Double) extends ScalaNumber {
97
protected def isWhole(): Boolean = value == value.toLong
98
def underlying(): Object = Double.box(value)
99
100
def intValue(): Int = value.toInt
101
def longValue(): Long = value.toLong
102
def floatValue(): Float = value.toFloat
103
def doubleValue(): Double = value
104
}
105
106
// Using hash functions
107
val hash1 = Statics.mix(0, 42)
108
val hash2 = Statics.longHash(123456789L)
109
val hash3 = Statics.doubleHash(3.14159)
110
val anyHash = Statics.anyHash("hello")
111
112
// Using JProcedure traits (Scala 3)
113
import scala.runtime.function.JProcedure1
114
115
class MyProcedure extends JProcedure1[String] {
116
def applyVoid(s: String): Unit = println(s"Processing: $s")
117
}
118
119
val proc = new MyProcedure()
120
proc.applyVoid("test") // Prints: Processing: test
121
val result = proc.apply("test") // Returns BoxedUnit.UNIT
122
```
123
124
## Hash Function Details
125
126
The `Statics` object provides optimized hash functions used internally by the Scala Native runtime:
127
128
- `mix` and `mixLast`: MurmurHash3-based mixing functions
129
- `finalizeHash` and `avalanche`: Hash finalization with bit avalanching
130
- Type-specific hash functions for `Long`, `Double`, `Float` with proper handling of special values
131
- `anyHash`: Universal hash function for any object with null safety
132
133
## JProcedure Trait Hierarchy
134
135
The JProcedure traits (available in Scala 3) provide Java interoperability for functions that return Unit:
136
137
- `JProcedure0` through `JProcedure22` support 0-22 parameters
138
- Each trait extends the corresponding Scala `Function` trait
139
- `applyVoid` method performs the actual Unit-returning operation
140
- `apply` method wraps the result in `BoxedUnit.UNIT` for Java compatibility