You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/semantics.md
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,7 @@ However, a few differences exist, which we mention here.
10
10
11
11
## Primitive data types
12
12
13
-
All primitive data types work exactly as on the JVM, with the following three
14
-
exceptions.
13
+
All nine primitive data types of Scala, i.e., `Boolean`, `Char`, `Byte`, `Short`, `Int`, `Long`, `Float`, `Double` and `Unit`, work exactly as on the JVM, with the following four exceptions.
15
14
16
15
### Floats can behave as Doubles by default
17
16
@@ -80,6 +79,21 @@ if strict-floats are enabled, or
80
79
81
80
otherwise.
82
81
82
+
### `getClass()`
83
+
84
+
In Scala/JVM as well as Scala.js, when assigning a primitive value to an `Any` (or a generic type), and asking for its `getClass()`, Scala returns the *boxed class* of the value's type, rather than the primitive value.
85
+
For example, `(true: Any).getClass()` returns `classOf[java.lang.Boolean]`, not `classOf[scala.Boolean]`.
86
+
87
+
In Scala.js, for numeric types, and for the same reason that instance tests are based on values, the result will be the *smallest* boxed class that can store the value.
88
+
Hence, `(5: Any).getClass()` will return `classOf[java.lang.Byte]`, while `(50000: Any).getClass()` will return `classOf[java.lang.Integer]`.
89
+
90
+
**Scala.js 1.x only:**
91
+
Moreover, for `()` (unit), the result will be `classOf[java.lang.Void]` instead of `classOf[scala.runtime.BoxedUnit]` like the JVM.
92
+
`scala.runtime.BoxedUnit` is an implementation detail of Scala on the JVM, which Scala.js does not emulate.
93
+
Instead, it uses the more sensible `java.lang.Void`, as `Void` is the boxed class corresponding to the `void` primitive type, which is `scala.Unit`.
94
+
This means that while `java.lang.Void` is not instantiable on the JVM, in Scala.js it has a singleton instance, namely `()`.
95
+
This also manifests itself in `Array[Unit]` which is effectively `Array[java.lang.Void]` at run-time, instead of `Array[scala.runtime.BoxedUnit]`.
96
+
83
97
## Undefined behaviors
84
98
85
99
The JVM is a very well specified environment, which even specifies how some
0 commit comments