-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add SIP 23 ValueOf #5647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add SIP 23 ValueOf #5647
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala | ||
|
||
/** | ||
* `ValueOf[T]` provides the unique value of the type `T` where `T` is a type which has a | ||
* single inhabitant. Eligible types are singleton types of the form `stablePath.type`, | ||
* Unit and singleton types corresponding to value literals. | ||
* | ||
* Instances of `ValueOf[T]` are provided implicitly for all eligible types. Typically | ||
* an instance would be required where a runtime value corresponding to a type level | ||
* computation is needed. | ||
|
||
* For example, we might define a type `Residue[M <: Int]` corresponding to the group of | ||
* integers modulo `M`. We could then mandate that residues can be summed only when they | ||
* are parameterized by the same modulus, | ||
* | ||
* {{{ | ||
* case class Residue[M <: Int](n: Int) extends AnyVal { | ||
* def +(rhs: Residue[M])(implicit m: ValueOf[M]): Residue[M] = | ||
* Residue((this.n + rhs.n) % valueOf[M]) | ||
* } | ||
* | ||
* val fiveModTen = Residue[10](5) | ||
* val nineModTen = Residue[10](9) | ||
* | ||
* fiveModTen + nineModTen // OK == Residue[10](4) | ||
* | ||
* val fourModEleven = Residue[11](4) | ||
* | ||
* fiveModTen + fourModEleven // compiler error: type mismatch; | ||
* // found : Residue[11] | ||
* // required: Residue[10] | ||
* }}} | ||
* | ||
* Notice that here the modulus is encoded in the type of the values and so does not | ||
* incur any additional per-value storage cost. When a runtime value of the modulus | ||
* is required in the implementation of `+` it is provided at the call site via the | ||
* implicit argument `m` of type `ValueOf[M]`. | ||
*/ | ||
@scala.annotation.implicitNotFound(msg = "No singleton value available for ${T}.") | ||
final class ValueOf[T](val value: T) extends AnyVal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
object Test { | ||
trait Foo0 { | ||
type T0 | ||
} | ||
|
||
object Foo0 { | ||
type Aux[T] = Foo0 {type T0 = T} | ||
implicit def apply[T](implicit v: ValueOf[T]): Aux[T] = new Foo0 { | ||
type T0 = T | ||
} | ||
} | ||
|
||
type Foo[T] = Foo0 { type T0 = T } | ||
val Foo = Foo0 | ||
|
||
Foo[5] | ||
implicitly[Foo.Aux[5]] | ||
implicitly[Foo[5]] | ||
|
||
|
||
val three: 3 = 3 | ||
type Three = three.type | ||
Foo[Three] | ||
implicitly[Foo.Aux[Three]] | ||
implicitly[Foo[Three]] | ||
|
||
final object bar | ||
type Bar = bar.type | ||
Foo[Bar] | ||
implicitly[Foo.Aux[Bar]] | ||
implicitly[Foo[Bar]] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
object Test extends App { | ||
object Foo | ||
val foo = "foo" | ||
|
||
implicitly[ValueOf[1]] | ||
implicitly[ValueOf[1L]] | ||
implicitly[ValueOf[1.0]] | ||
implicitly[ValueOf[1.0F]] | ||
implicitly[ValueOf[true]] | ||
implicitly[ValueOf['f']] | ||
implicitly[ValueOf["foo"]] | ||
implicitly[ValueOf[Unit]] | ||
implicitly[ValueOf[Foo.type]] | ||
implicitly[ValueOf[foo.type]] | ||
|
||
assert((valueOf[1]: 1) == 1) | ||
assert((valueOf[1L]: 1L) == 1L) | ||
assert((valueOf[1.0]: 1.0) == 1.0) | ||
assert((valueOf[1.0F]: 1.0F) == 1.0F) | ||
assert((valueOf[true]: true) == true) | ||
assert((valueOf['f']: 'f') == 'f') | ||
assert((valueOf["foo"]: "foo") == "foo") | ||
assert((valueOf[Unit]: Unit) == ((): Any)) | ||
assert((valueOf[Foo.type]: Foo.type) eq Foo) | ||
assert((valueOf[foo.type]: foo.type) eq foo) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
Unit
handled by theContantType
case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it should be, but currently
Unit
isn't classified as a singleton type in either Scala 2 or Dotty.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should certainly be, at least conceptually. Isn't
Unit
the singleton type par excellence?