@@ -89,18 +89,21 @@ Lightbend Scala compiler.
89
89
```
90
90
91
91
+ The ` .type ` singleton type forming operator can be applied to values of all subtypes of ` Any ` .
92
+ To prevent the compiler from widening our return type we assign to a final val.
92
93
```
93
94
def foo[T](t: T): t.type = t
94
- foo(23) // result is 23 : 23
95
+ final val bar = foo(23) // result is bar : 23
95
96
```
96
97
97
98
+ The presence of an upper bound of ` Singleton ` on a formal type parameter indicates that
98
- singleton types should be inferred for type parameters at call sites.
99
+ singleton types should be inferred for type parameters at call sites. To help see this
100
+ we introduce type constructor ` Id ` to prevent the compiler from widening our return type.
99
101
```
100
- def wide[T](t: T): T = t
101
- wide(13) // result is 13: Int
102
- def narrow[T <: Singleton](t: T): T = t
103
- narrow(23) // result is 23: 23
102
+ type Id[A] = A
103
+ def wide[T](t: T): Id[T] = t
104
+ wide(23) // result is 23: Id[Int]
105
+ def narrow[T <: Singleton](t: T): Id[T] = t
106
+ narrow(23) // result is 23: Id[23]
104
107
```
105
108
106
109
+ Pattern matching against literal types and ` isInstanceOf ` tests are
@@ -109,15 +112,15 @@ Lightbend Scala compiler.
109
112
(1: Any) match {
110
113
case one: 1 => true
111
114
case _ => false
112
- } // result is true
113
- (1: Any).isInstanceOf[1] // result is true
115
+ } // result is true: Boolean
116
+ (1: Any).isInstanceOf[1] // result is true: Boolean
114
117
```
115
118
116
119
+ A ` scala.ValueOf[T] ` type class and corresponding ` scala.Predef.valueOf[T] ` operator has been
117
120
added yielding the unique value of types with a single inhabitant.
118
121
```
119
122
def foo[T](implicit v: ValueOf[T]): T = v.value
120
- foo[13] // result is 13: 13
123
+ foo[13] // result is 13: Int
121
124
```
122
125
123
126
@@ -384,7 +387,7 @@ applications which work with large datasets.
384
387
Example,
385
388
```
386
389
def foo[ T] (t: T): t.type = t
387
- foo(23) // result is 23 : 23
390
+ final val bar = foo(23) // result is bar : 23
388
391
```
389
392
390
393
+ The presence of an upper bound of `Singleton` on a formal type parameter indicates that
@@ -469,12 +472,15 @@ applications which work with large datasets.
469
472
470
473
Example,
471
474
```
472
- def wide[ T] (t: T): T = t
473
- wide(13) // result is 13: Int
474
- def narrow[ T <: Singleton] (t: T): T = t
475
- narrow(23) // result is 23: 23
475
+ type Id[ A] = A
476
+ def wide[ T] (t: T): Id[ T] = t
477
+ wide(23) // result is 23: Id[ Int]
478
+ def narrow[ T <: Singleton] (t: T): Id[ T] = t
479
+ narrow(23) // result is 23: Id[ 23]
476
480
```
477
481
482
+ Note that we introduce the type constructor `Id` simply to avoid widening of the return type.
483
+
478
484
+ A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been
479
485
added yielding the unique value of types with a single inhabitant.
480
486
@@ -498,7 +504,7 @@ applications which work with large datasets.
498
504
Example,
499
505
```
500
506
def foo[ T] (implicit v: ValueOf[ T] ): T = v.value
501
- foo[ 13] // result is 13: 13
507
+ foo[ 13] // result is 13: Int
502
508
```
503
509
504
510
A method `valueOf` is also added to `scala.Predef` analogously to existing operators such as
@@ -513,7 +519,7 @@ applications which work with large datasets.
513
519
object Foo
514
520
valueOf[ Foo.type] // result is Foo: Foo.type
515
521
516
- valueOf[ 23] // result is 23: 23
522
+ valueOf[ 23] // result is 23: Int
517
523
```
518
524
519
525
+ Pattern matching against literal types and `isInstanceOf` tests are
@@ -533,8 +539,8 @@ applications which work with large datasets.
533
539
(1: Any) match {
534
540
case one: 1 => true
535
541
case _ => false
536
- } // result is true
537
- (1: Any).isInstanceOf[ 1] // result is true
542
+ } // result is true: Boolean
543
+ (1: Any).isInstanceOf[ 1] // result is true: Boolean
538
544
```
539
545
540
546
Importantly, that doesn't include `asInstanceOf` as that is a user assertion to the compiler, with
0 commit comments