From c0d730ddd8645c3db2d4e9b40897412cf8c417cc Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Mon, 9 Dec 2019 16:19:28 -0500 Subject: [PATCH 01/10] Remove proposal example on `.type` --- _sips/sips/2014-06-27-42.type.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 2b5ce23fd5..88d4aa5741 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -88,12 +88,6 @@ Lightbend Scala compiler. foo(1: 1) // type ascription ``` -+ The `.type` singleton type forming operator can be applied to values of all subtypes of `Any`. - ``` - def foo[T](t: T): t.type = t - foo(23) // result is 23: 23 - ``` - + The presence of an upper bound of `Singleton` on a formal type parameter indicates that singleton types should be inferred for type parameters at call sites. ``` From 9b6ace306691b92c17fcb881e82ee0d8d3b2e645 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Mon, 9 Dec 2019 16:20:08 -0500 Subject: [PATCH 02/10] Change section on Singleton to use Id --- _sips/sips/2014-06-27-42.type.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 88d4aa5741..7ae3c3236c 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -89,12 +89,14 @@ Lightbend Scala compiler. ``` + The presence of an upper bound of `Singleton` on a formal type parameter indicates that - singleton types should be inferred for type parameters at call sites. - ``` - def wide[T](t: T): T = t - wide(13) // result is 13: Int - def narrow[T <: Singleton](t: T): T = t - narrow(23) // result is 23: 23 + singleton types should be inferred for type parameters at call sites. To help see this + we introduce type constructor `Id` to prevent the compiler from widening our return type. + ``` + type Id[A] = A + def wide[T](t: T): Id[T] = t + wide(23) // result is Id[Int] = 23 + def narrow[T <: Singleton](t: T): Id[T] = t + narrow(23) // result is Id[23] = 23 ``` + Pattern matching against literal types and `isInstanceOf` tests are From 2302399a2baae36519646c8737d14baba8d62295 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Mon, 9 Dec 2019 16:21:05 -0500 Subject: [PATCH 03/10] Fix return type in comment regarding ValueOf --- _sips/sips/2014-06-27-42.type.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 7ae3c3236c..40029cfe79 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -113,7 +113,7 @@ Lightbend Scala compiler. added yielding the unique value of types with a single inhabitant. ``` def foo[T](implicit v: ValueOf[T]): T = v.value - foo[13] // result is 13: 13 + foo[13] // result is Int = 13 ``` From 2ce46ea9d1cc7ff8fb429ee32c45ce8b9f72f33e Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 22:10:12 -0500 Subject: [PATCH 04/10] Revert "Remove proposal example on `.type`" This reverts commit c0d730ddd8645c3db2d4e9b40897412cf8c417cc. --- _sips/sips/2014-06-27-42.type.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 40029cfe79..f2fb9afdc1 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -88,6 +88,12 @@ Lightbend Scala compiler. foo(1: 1) // type ascription ``` ++ The `.type` singleton type forming operator can be applied to values of all subtypes of `Any`. + ``` + def foo[T](t: T): t.type = t + foo(23) // result is 23: 23 + ``` + + The presence of an upper bound of `Singleton` on a formal type parameter indicates that singleton types should be inferred for type parameters at call sites. To help see this we introduce type constructor `Id` to prevent the compiler from widening our return type. From 71261c4b4dcdc3128157bf09b3379128afc3f715 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 22:17:31 -0500 Subject: [PATCH 05/10] Tweak .type example to prevent widening --- _sips/sips/2014-06-27-42.type.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index f2fb9afdc1..802e7dcf32 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -89,9 +89,10 @@ Lightbend Scala compiler. ``` + The `.type` singleton type forming operator can be applied to values of all subtypes of `Any`. + To prevent the compiler from widening our return type we assign to a final val. ``` def foo[T](t: T): t.type = t - foo(23) // result is 23: 23 + final val bar = foo(23) // result is bar: 23 = 23 ``` + The presence of an upper bound of `Singleton` on a formal type parameter indicates that @@ -386,7 +387,7 @@ applications which work with large datasets. Example, ``` def foo[T](t: T): t.type = t - foo(23) // result is 23: 23 + final val bar = foo(23) // result is bar: 23 = 23 ``` + The presence of an upper bound of `Singleton` on a formal type parameter indicates that From 68d86c5b32ff789e9458a45d7a9dc8b7fa3b5f6b Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 22:48:18 -0500 Subject: [PATCH 06/10] Change Singleton example in details to use Id --- _sips/sips/2014-06-27-42.type.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 802e7dcf32..529b48d151 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -473,11 +473,14 @@ applications which work with large datasets. Example, ``` def wide[T](t: T): T = t - wide(13) // result is 13: Int - def narrow[T <: Singleton](t: T): T = t - narrow(23) // result is 23: 23 + wide(13) // result is Int = 13 + type Id[A] = A + def narrow[T <: Singleton](t: T): Id[T] = t + narrow(23) // result is Id[23] = 23 ``` + Note that we introduce the type constructor `Id` simply to avoid widening of the return type. + + A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been added yielding the unique value of types with a single inhabitant. From d52dfe8463fbd71d0782ac6ee0e38b9fa861ed19 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 22:58:22 -0500 Subject: [PATCH 07/10] Update ValueOf examples --- _sips/sips/2014-06-27-42.type.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 529b48d151..481c37c232 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -504,7 +504,7 @@ applications which work with large datasets. Example, ``` def foo[T](implicit v: ValueOf[T]): T = v.value - foo[13] // result is 13: 13 + foo[13] // result is Int = 13 ``` A method `valueOf` is also added to `scala.Predef` analogously to existing operators such as @@ -517,9 +517,9 @@ applications which work with large datasets. Example, ``` object Foo - valueOf[Foo.type] // result is Foo: Foo.type + valueOf[Foo.type] // result is Foo = Foo.type - valueOf[23] // result is 23: 23 + valueOf[23] // result is Int = 23 ``` + Pattern matching against literal types and `isInstanceOf` tests are From af2d35e6f15a14ce931a9e3d1d46802dc3b41dca Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 23:02:26 -0500 Subject: [PATCH 08/10] Update remaining result comments to match repl --- _sips/sips/2014-06-27-42.type.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 481c37c232..29df69a6b4 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -141,7 +141,7 @@ val one: wOne.T = wOne.value // wOne.T is the type 1 // wOne.value is 1: 1 def foo[T](implicit w: Witness[T]): w.T = w.value -foo[wOne.T] // result is 1: 1 +foo[wOne.T] // result is 1 = 1 "foo" ->> 23 // shapeless record field constructor // result type is FieldType["foo", Int] @@ -451,7 +451,7 @@ applications which work with large datasets. class Widen object Narrow extends Wide def id[T](t: T): T = t - id(Narrow) // result is Narrow: Narrow.type + id(Narrow) // result is Narrow = Narrow.type ``` This SIP updates the specification to match the current implementation and then adds the further From 3867f3d2a4a5d33d8a45b1e4f9b9a5b591b10bf9 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 23:13:03 -0500 Subject: [PATCH 09/10] Change result comments to match type ascriptions --- _sips/sips/2014-06-27-42.type.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 29df69a6b4..2db68db9e4 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -92,7 +92,7 @@ Lightbend Scala compiler. To prevent the compiler from widening our return type we assign to a final val. ``` def foo[T](t: T): t.type = t - final val bar = foo(23) // result is bar: 23 = 23 + final val bar = foo(23) // result is bar: 23 ``` + The presence of an upper bound of `Singleton` on a formal type parameter indicates that @@ -101,9 +101,9 @@ Lightbend Scala compiler. ``` type Id[A] = A def wide[T](t: T): Id[T] = t - wide(23) // result is Id[Int] = 23 + wide(23) // result is 23: Id[Int] def narrow[T <: Singleton](t: T): Id[T] = t - narrow(23) // result is Id[23] = 23 + narrow(23) // result is 23: Id[23] ``` + Pattern matching against literal types and `isInstanceOf` tests are @@ -112,15 +112,15 @@ Lightbend Scala compiler. (1: Any) match { case one: 1 => true case _ => false - } // result is true - (1: Any).isInstanceOf[1] // result is true + } // result is true: Boolean + (1: Any).isInstanceOf[1] // result is true: Boolean ``` + A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been added yielding the unique value of types with a single inhabitant. ``` def foo[T](implicit v: ValueOf[T]): T = v.value - foo[13] // result is Int = 13 + foo[13] // result is 13: Int ``` @@ -141,7 +141,7 @@ val one: wOne.T = wOne.value // wOne.T is the type 1 // wOne.value is 1: 1 def foo[T](implicit w: Witness[T]): w.T = w.value -foo[wOne.T] // result is 1 = 1 +foo[wOne.T] // result is 1: 1 "foo" ->> 23 // shapeless record field constructor // result type is FieldType["foo", Int] @@ -387,7 +387,7 @@ applications which work with large datasets. Example, ``` def foo[T](t: T): t.type = t - final val bar = foo(23) // result is bar: 23 = 23 + final val bar = foo(23) // result is bar: 23 ``` + The presence of an upper bound of `Singleton` on a formal type parameter indicates that @@ -451,7 +451,7 @@ applications which work with large datasets. class Widen object Narrow extends Wide def id[T](t: T): T = t - id(Narrow) // result is Narrow = Narrow.type + id(Narrow) // result is Narrow: Narrow.type ``` This SIP updates the specification to match the current implementation and then adds the further @@ -473,10 +473,10 @@ applications which work with large datasets. Example, ``` def wide[T](t: T): T = t - wide(13) // result is Int = 13 + wide(13) // result is 13: Int type Id[A] = A def narrow[T <: Singleton](t: T): Id[T] = t - narrow(23) // result is Id[23] = 23 + narrow(23) // result is 23: Id[23] ``` Note that we introduce the type constructor `Id` simply to avoid widening of the return type. @@ -504,7 +504,7 @@ applications which work with large datasets. Example, ``` def foo[T](implicit v: ValueOf[T]): T = v.value - foo[13] // result is Int = 13 + foo[13] // result is 13: Int ``` A method `valueOf` is also added to `scala.Predef` analogously to existing operators such as @@ -517,9 +517,9 @@ applications which work with large datasets. Example, ``` object Foo - valueOf[Foo.type] // result is Foo = Foo.type + valueOf[Foo.type] // result is Foo: Foo.type - valueOf[23] // result is Int = 23 + valueOf[23] // result is 23: Int ``` + Pattern matching against literal types and `isInstanceOf` tests are @@ -539,8 +539,8 @@ applications which work with large datasets. (1: Any) match { case one: 1 => true case _ => false - } // result is true - (1: Any).isInstanceOf[1] // result is true + } // result is true: Boolean + (1: Any).isInstanceOf[1] // result is true: Boolean ``` Importantly, that doesn't include `asInstanceOf` as that is a user assertion to the compiler, with From 19d51a11a48516cdb75bd93615191b657aeaf734 Mon Sep 17 00:00:00 2001 From: Andrew Valencik Date: Wed, 11 Dec 2019 23:16:27 -0500 Subject: [PATCH 10/10] Make both Singleton examples match --- _sips/sips/2014-06-27-42.type.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index 2db68db9e4..593a6ad6b7 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -472,9 +472,9 @@ applications which work with large datasets. Example, ``` - def wide[T](t: T): T = t - wide(13) // result is 13: Int type Id[A] = A + def wide[T](t: T): Id[T] = t + wide(23) // result is 23: Id[Int] def narrow[T <: Singleton](t: T): Id[T] = t narrow(23) // result is 23: Id[23] ```