0
# Reference Type Wrappers
1
2
Mutable and volatile reference wrappers for all primitive types and objects. These types provide thread-safe access patterns and support for atomic operations, enabling efficient mutable variable handling in Scala Native.
3
4
## Core Imports
5
6
```scala
7
import scala.runtime.{BooleanRef, CharRef, ByteRef, ShortRef, IntRef, LongRef, FloatRef, DoubleRef, ObjectRef}
8
import scala.runtime.{VolatileBooleanRef, VolatileCharRef, VolatileByteRef, VolatileShortRef, VolatileIntRef, VolatileLongRef, VolatileFloatRef, VolatileDoubleRef, VolatileObjectRef}
9
```
10
11
## Capabilities
12
13
### Boolean References
14
15
Mutable and volatile boolean reference types.
16
17
```scala { .api }
18
class BooleanRef(var elem: Boolean) extends Serializable {
19
override def toString(): String
20
}
21
22
object BooleanRef {
23
def create(elem: Boolean): BooleanRef
24
def zero(): BooleanRef
25
}
26
27
class VolatileBooleanRef(@volatile var elem: Boolean) extends Serializable {
28
override def toString(): String
29
}
30
31
object VolatileBooleanRef {
32
def create(elem: Boolean): VolatileBooleanRef
33
def zero(): VolatileBooleanRef
34
}
35
```
36
37
### Character References
38
39
Mutable and volatile character reference types.
40
41
```scala { .api }
42
class CharRef(var elem: Char) extends Serializable {
43
override def toString(): String
44
}
45
46
object CharRef {
47
def create(elem: Char): CharRef
48
def zero(): CharRef
49
}
50
51
class VolatileCharRef(@volatile var elem: Char) extends Serializable {
52
override def toString(): String
53
}
54
55
object VolatileCharRef {
56
def create(elem: Char): VolatileCharRef
57
def zero(): VolatileCharRef
58
}
59
```
60
61
### Byte References
62
63
Mutable and volatile byte reference types.
64
65
```scala { .api }
66
class ByteRef(var elem: Byte) extends Serializable {
67
override def toString(): String
68
}
69
70
object ByteRef {
71
def create(elem: Byte): ByteRef
72
def zero(): ByteRef
73
}
74
75
class VolatileByteRef(@volatile var elem: Byte) extends Serializable {
76
override def toString(): String
77
}
78
79
object VolatileByteRef {
80
def create(elem: Byte): VolatileByteRef
81
def zero(): VolatileByteRef
82
}
83
```
84
85
### Short References
86
87
Mutable and volatile short reference types.
88
89
```scala { .api }
90
class ShortRef(var elem: Short) extends Serializable {
91
override def toString(): String
92
}
93
94
object ShortRef {
95
def create(elem: Short): ShortRef
96
def zero(): ShortRef
97
}
98
99
class VolatileShortRef(@volatile var elem: Short) extends Serializable {
100
override def toString(): String
101
}
102
103
object VolatileShortRef {
104
def create(elem: Short): VolatileShortRef
105
def zero(): VolatileShortRef
106
}
107
```
108
109
### Integer References
110
111
Mutable and volatile integer reference types.
112
113
```scala { .api }
114
class IntRef(var elem: Int) extends Serializable {
115
override def toString(): String
116
}
117
118
object IntRef {
119
def create(elem: Int): IntRef
120
def zero(): IntRef
121
}
122
123
class VolatileIntRef(@volatile var elem: Int) extends Serializable {
124
override def toString(): String
125
}
126
127
object VolatileIntRef {
128
def create(elem: Int): VolatileIntRef
129
def zero(): VolatileIntRef
130
}
131
```
132
133
### Long References
134
135
Mutable and volatile long reference types.
136
137
```scala { .api }
138
class LongRef(var elem: Long) extends Serializable {
139
override def toString(): String
140
}
141
142
object LongRef {
143
def create(elem: Long): LongRef
144
def zero(): LongRef
145
}
146
147
class VolatileLongRef(@volatile var elem: Long) extends Serializable {
148
override def toString(): String
149
}
150
151
object VolatileLongRef {
152
def create(elem: Long): VolatileLongRef
153
def zero(): VolatileLongRef
154
}
155
```
156
157
### Float References
158
159
Mutable and volatile float reference types.
160
161
```scala { .api }
162
class FloatRef(var elem: Float) extends Serializable {
163
override def toString(): String
164
}
165
166
object FloatRef {
167
def create(elem: Float): FloatRef
168
def zero(): FloatRef
169
}
170
171
class VolatileFloatRef(@volatile var elem: Float) extends Serializable {
172
override def toString(): String
173
}
174
175
object VolatileFloatRef {
176
def create(elem: Float): VolatileFloatRef
177
def zero(): VolatileFloatRef
178
}
179
```
180
181
### Double References
182
183
Mutable and volatile double reference types.
184
185
```scala { .api }
186
class DoubleRef(var elem: Double) extends Serializable {
187
override def toString(): String
188
}
189
190
object DoubleRef {
191
def create(elem: Double): DoubleRef
192
def zero(): DoubleRef
193
}
194
195
class VolatileDoubleRef(@volatile var elem: Double) extends Serializable {
196
override def toString(): String
197
}
198
199
object VolatileDoubleRef {
200
def create(elem: Double): VolatileDoubleRef
201
def zero(): VolatileDoubleRef
202
}
203
```
204
205
### Object References
206
207
Generic mutable and volatile object reference types.
208
209
```scala { .api }
210
class ObjectRef[A](var elem: A) extends Serializable {
211
override def toString(): String
212
}
213
214
object ObjectRef {
215
def create[A](elem: A): ObjectRef[A]
216
def zero(): ObjectRef[Object]
217
}
218
219
class VolatileObjectRef[A](@volatile var elem: A) extends Serializable {
220
override def toString(): String
221
}
222
223
object VolatileObjectRef {
224
def create[A](elem: A): VolatileObjectRef[A]
225
def zero(): VolatileObjectRef[Object]
226
}
227
```
228
229
## Usage Examples
230
231
```scala
232
import scala.runtime.{BooleanRef, IntRef, ObjectRef, VolatileIntRef}
233
234
// Create mutable references
235
val boolRef = BooleanRef.create(true)
236
val intRef = IntRef.create(42)
237
val objRef = ObjectRef.create("hello")
238
239
// Modify values
240
boolRef.elem = false
241
intRef.elem = 100
242
objRef.elem = "world"
243
244
// Create zero-initialized references
245
val zeroBool = BooleanRef.zero() // false
246
val zeroInt = IntRef.zero() // 0
247
val zeroObj = ObjectRef.zero() // null
248
249
// Volatile references for thread-safe access
250
val volatileInt = VolatileIntRef.create(10)
251
volatileInt.elem = 20 // Thread-safe assignment
252
253
// String representation
254
println(intRef.toString()) // "42"
255
```
256
257
## Thread Safety
258
259
Regular reference types (`BooleanRef`, `IntRef`, etc.) provide basic mutable references without built-in thread safety. Volatile reference types (`VolatileBooleanRef`, `VolatileIntRef`, etc.) use the `@volatile` annotation to ensure that reads and writes are immediately visible across threads, providing memory visibility guarantees without full synchronization.