File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change @@ -238,20 +238,19 @@ Value types can be cast in the following way:
238
238
For example:
239
239
240
240
``` scala
241
- val x : Long = 987654321
242
- val y : Float = x // 9.8765434E8 (note that some precision is lost in this case)
241
+ val b : Byte = 127
242
+ val i : Int = b // 127
243
243
244
244
val face : Char = '☺'
245
245
val number : Int = face // 9786
246
246
```
247
247
248
- Casting is unidirectional.
249
- This will not compile:
248
+ You can only cast to a type if there is no loss of information. Otherwise, you need to be explicit about the cast:
250
249
251
- ```
250
+ ``` scala
252
251
val x : Long = 987654321
253
- val y: Float = x // 9.8765434E8
254
- val z: Long = y // Does not conform
252
+ val y : Float = x.toFloat // 9.8765434E8 (note that `.toFloat` is required because the cast results in percision loss)
253
+ val z : Long = y // Error
255
254
```
256
255
257
256
You can also cast a reference type to a subtype.
You can’t perform that action at this time.
0 commit comments