Skip to content

Commit 0eb7267

Browse files
committed
value-classes: Fix int-as-Any example
Fixes Kotlin#266
1 parent cb297b0 commit 0eb7267

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

notes/value-classes.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ in the Kotlin standard library does not specify its behavior with respect to ret
122122
123123
### Built-in primitive value classes
124124

125-
Since its inception, Kotlin has a number of primitive built-in standard library classes `Int`, `Long`, `Double`, etc that don’t have a stable identity at all. They behave like regular classes for all the purposes with the exception that they don’t have the very concept of identity, and the reference equality operator (`===`) for them is deprecated (and will be removed in the future). They are prime examples of _value classes_. To see that they don’t have a stable identity you can cast them to a reference-based `Any` type. This forces a value class to be boxed, creating a temporary identity object. Having done that, you can apply a reference equality operation to the result
126-
([playground](https://pl.kotl.in/pq8ftp4mZ)):
125+
Since its inception, Kotlin has a number of primitive built-in standard library classes `Int`, `Long`, `Double`, etc that don’t have a stable identity at all. They behave like regular classes for all the purposes with the exception that they don’t have the very concept of identity, and the reference equality operator (`===`) for them is deprecated (and will be removed in the future). They are prime examples of _value classes_. To see that they don’t have a stable identity you can coerce them to a reference-based `Any` type. This forces a value class to be boxed, creating a temporary identity object. Having done that, you can apply a reference equality operation to the result
126+
([playground](https://pl.kotl.in/gIhIZS4d0)):
127127

128128
```kotlin
129129
fun main() {
130130
val a = 2021
131131
val b = a
132-
println(a == b) // true -- same value
133-
println((a as Any) === (b as Any)) // false -- different instance
132+
val aRef: Any = a // coerce a value to Any
133+
val bRef: Any = b // coerce b value to Any
134+
println(aRef == bRef) // true -- same value
135+
println(aRef === bRef) // false -- different instance
134136
}
135137
```
136138

0 commit comments

Comments
 (0)