Skip to content

Commit b4e7f22

Browse files
valencikSethTisue
authored andcommitted
Fix SIP-23 (Literal Types) (#1599)
* Change section on Singleton to use Id * Fix return type in comment regarding ValueOf * Tweak .type example to prevent widening * Update ValueOf examples * Update remaining result comments to match repl * Change result comments to match type ascriptions * Make both Singleton examples match
1 parent 10f96f3 commit b4e7f22

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

_sips/sips/2014-06-27-42.type.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,21 @@ Lightbend Scala compiler.
8989
```
9090

9191
+ 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.
9293
```
9394
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
9596
```
9697

9798
+ 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.
99101
```
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]
104107
```
105108

106109
+ Pattern matching against literal types and `isInstanceOf` tests are
@@ -109,15 +112,15 @@ Lightbend Scala compiler.
109112
(1: Any) match {
110113
case one: 1 => true
111114
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
114117
```
115118

116119
+ A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been
117120
added yielding the unique value of types with a single inhabitant.
118121
```
119122
def foo[T](implicit v: ValueOf[T]): T = v.value
120-
foo[13] // result is 13: 13
123+
foo[13] // result is 13: Int
121124
```
122125

123126

@@ -384,7 +387,7 @@ applications which work with large datasets.
384387
Example,
385388
```
386389
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
388391
```
389392
390393
+ 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.
469472
470473
Example,
471474
```
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]
476480
```
477481
482+
Note that we introduce the type constructor `Id` simply to avoid widening of the return type.
483+
478484
+ A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been
479485
added yielding the unique value of types with a single inhabitant.
480486
@@ -498,7 +504,7 @@ applications which work with large datasets.
498504
Example,
499505
```
500506
def foo[T](implicit v: ValueOf[T]): T = v.value
501-
foo[13] // result is 13: 13
507+
foo[13] // result is 13: Int
502508
```
503509
504510
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.
513519
object Foo
514520
valueOf[Foo.type] // result is Foo: Foo.type
515521

516-
valueOf[23] // result is 23: 23
522+
valueOf[23] // result is 23: Int
517523
```
518524
519525
+ Pattern matching against literal types and `isInstanceOf` tests are
@@ -533,8 +539,8 @@ applications which work with large datasets.
533539
(1: Any) match {
534540
case one: 1 => true
535541
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
538544
```
539545
540546
Importantly, that doesn't include `asInstanceOf` as that is a user assertion to the compiler, with

0 commit comments

Comments
 (0)