0
# Scala Native Standard Library Overrides (scalalib)
1
2
Native-specific implementations that replace or enhance standard Scala library components for the Scala Native runtime. This module provides platform-optimized versions of select Scala standard library components, improving performance and compatibility with native compilation.
3
4
## Package Information
5
6
- **Package Name**: scalalib_native0.5_2.13
7
- **Package Type**: Maven/SBT
8
- **Language**: Scala (2.13, with support for 2.12 and 3.x)
9
- **Platform**: Scala Native 0.5.x
10
- **Installation**: `libraryDependencies += "org.scala-native" %%% "scalalib" % "0.5.8"`
11
12
## Core Imports
13
14
The scalalib overrides are automatically used when the corresponding standard library classes are imported:
15
16
```scala
17
// Enumeration override is used automatically
18
import scala.Enumeration
19
20
// VM memory management utilities (package-private)
21
import scala.collection.immutable.VM
22
```
23
24
## Basic Usage
25
26
```scala
27
// Enumeration works identically to standard Scala but with native optimizations
28
object Color extends Enumeration {
29
type Color = Value
30
val Red, Green, Blue = Value
31
}
32
33
val color = Color.Red
34
val allColors = Color.values
35
36
// VM memory management (used internally by collections)
37
import scala.collection.immutable.VM
38
VM.releaseFence() // Atomic memory fence operation
39
```
40
41
## Architecture
42
43
The scalalib module provides only two key override components:
44
45
- **VM Memory Management**: Atomic memory fence operations for thread-safe collection operations
46
- **Enhanced Enumeration**: Native-optimized enumeration implementation with improved performance and Scala.js compatibility
47
48
This focused approach ensures that critical standard library components have native-specific optimizations while maintaining full API compatibility with standard Scala implementations.
49
50
## Capabilities
51
52
### VM Memory Management
53
54
Package-private atomic memory fence operations for internal use by immutable collections, providing thread-safe memory synchronization in the native runtime environment.
55
56
```scala { .api }
57
package scala.collection.immutable
58
59
/* private[immutable] */ object VM {
60
def releaseFence(): Unit
61
}
62
```
63
64
[VM Memory Management](./native-overrides.md#vm-memory-management)
65
66
### Enhanced Enumeration Implementation
67
68
Complete native-optimized implementation of Scala enumerations with improved performance, better memory management, and Scala.js compatibility.
69
70
```scala { .api }
71
abstract class Enumeration(initial: Int) extends Serializable {
72
def this(): Enumeration
73
def values: ValueSet
74
def apply(x: Int): Value
75
def withName(s: String): Value
76
def maxId: Int
77
protected def Value: Value
78
protected def Value(i: Int): Value
79
protected def Value(name: String): Value
80
protected def Value(i: Int, name: String): Value
81
}
82
83
abstract class Enumeration#Value extends Ordered[Value] with Serializable {
84
def id: Int
85
def compare(that: Value): Int
86
def +(v: Value): ValueSet
87
}
88
89
class Enumeration#ValueSet extends immutable.SortedSet[Value] with Serializable {
90
def contains(v: Value): Boolean
91
def +(value: Value): ValueSet
92
def -(value: Value): ValueSet
93
def iterator: Iterator[Value]
94
def toBitMask: Array[Long]
95
}
96
```
97
98
[Enhanced Enumeration](./native-overrides.md#enhanced-enumeration-implementation)
99
100
## Types
101
102
### Core Enumeration Types
103
104
```scala { .api }
105
// ValueSet factory
106
object Enumeration#ValueSet {
107
val empty: ValueSet
108
def apply(elems: Value*): ValueSet
109
def fromBitMask(elems: Array[Long]): ValueSet
110
def newBuilder: mutable.Builder[Value, ValueSet]
111
}
112
113
// Internal Val implementation
114
protected class Enumeration#Val(i: Int, name: String) extends Value with Serializable {
115
def this(i: Int): Val
116
def this(name: String): Val
117
def this(): Val
118
def id: Int
119
}
120
121
// Value ordering
122
object Enumeration#ValueOrdering extends Ordering[Value] {
123
def compare(x: Value, y: Value): Int
124
}
125
```