-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add primitive compiletime operations on singleton types #7628
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
OlivierBlanvillain
merged 17 commits into
scala:master
from
MaximeKjaer:singleton-arithmetic
Jan 8, 2020
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1d3e473
Add primitive compiletime operations on singleton types
MaximeKjaer eabd95a
Fix isCompiletimeAppliedType for NoDenotation types
MaximeKjaer 30d0a8e
Fix infinite loop in normalization
MaximeKjaer b458372
Add singleton ops tests suggested by review
MaximeKjaer cb3ab69
Move all singleton ops to scala.compiletime.ops package
MaximeKjaer 4d73d89
Add support for non-singleton arguments in singleton ops
MaximeKjaer f634215
Add @infix annotation to infix singleton ops
MaximeKjaer c9a296d
Fix >:> comparison of compiletime applied types
MaximeKjaer 90696e9
Add error for illegal singleton ops
MaximeKjaer 9711dcc
Revert to only reducing singleton args in singleton ops
MaximeKjaer c355424
Add ToString singleton op on Ints
MaximeKjaer 5e287cd
Address reviewer feedback
MaximeKjaer 7b9ccb4
Add singleton string concatenation
MaximeKjaer da71e1d
Move compiletime ops into subpackages
MaximeKjaer 2cab591
Add compiletime ops documentation
MaximeKjaer 8c117c1
Move equality singleton ops to scala.compiletime.ops.any
MaximeKjaer 7e0a1db
Address reviewer feedback about singleton ops code documentation
MaximeKjaer 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
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,38 @@ | ||
| package scala.compiletime | ||
|
|
||
| import scala.annotation.infix | ||
|
|
||
| package object ops { | ||
| @infix type ==[X <: AnyVal, Y <: AnyVal] <: Boolean | ||
| @infix type !=[X <: AnyVal, Y <: AnyVal] <: Boolean | ||
|
|
||
| object string { | ||
| @infix type +[X <: String, Y <: String] <: String | ||
| } | ||
|
|
||
| object int { | ||
| @infix type +[X <: Int, Y <: Int] <: Int | ||
| @infix type -[X <: Int, Y <: Int] <: Int | ||
| @infix type *[X <: Int, Y <: Int] <: Int | ||
| @infix type /[X <: Int, Y <: Int] <: Int | ||
| @infix type %[X <: Int, Y <: Int] <: Int | ||
|
|
||
| @infix type <[X <: Int, Y <: Int] <: Boolean | ||
| @infix type >[X <: Int, Y <: Int] <: Boolean | ||
| @infix type >=[X <: Int, Y <: Int] <: Boolean | ||
| @infix type <=[X <: Int, Y <: Int] <: Boolean | ||
|
|
||
| type Abs[X <: Int] <: Int | ||
| type Negate[X <: Int] <: Int | ||
| type Min[X <: Int, Y <: Int] <: Int | ||
| type Max[X <: Int, Y <: Int] <: Int | ||
| type ToString[X <: Int] <: String | ||
| } | ||
|
|
||
| object boolean { | ||
| type ![X <: Boolean] <: Boolean | ||
| @infix type ^[X <: Boolean, Y <: Boolean] <: Boolean | ||
| @infix type &&[X <: Boolean, Y <: Boolean] <: Boolean | ||
| @infix type ||[X <: Boolean, Y <: Boolean] <: Boolean | ||
| } | ||
| } | ||
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,23 @@ | ||
| import scala.compiletime.ops.boolean._ | ||
|
|
||
| object Test { | ||
| val t0: ![true] = false | ||
| val t1: ![false] = true | ||
| val t2: ![true] = true // error | ||
| val t3: ![false] = false // error | ||
|
|
||
| val t4: true && true = true | ||
| val t5: true && false = false | ||
| val t6: false && true = true // error | ||
| val t7: false && false = true // error | ||
|
|
||
| val t8: true ^ true = false | ||
| val t9: false ^ true = true | ||
| val t10: false ^ false = true // error | ||
| val t11: true ^ false = false // error | ||
|
|
||
| val t12: true || true = true | ||
| val t13: true || false = true | ||
| val t14 false || true = false // error | ||
| val t15: false || false = true // error | ||
| } |
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,75 @@ | ||
| import scala.compiletime.ops.int._ | ||
|
|
||
| object Test { | ||
| summon[2 + 3 =:= 6 - 1] | ||
| summon[1763 =:= 41 * 43] | ||
| summon[2 + 2 =:= 3] // error | ||
| summon[29 * 31 =:= 900] // error | ||
| summon[Int <:< Int + 1] // error | ||
| summon[1 + Int <:< Int] | ||
|
|
||
| val t0: 2 + 3 = 5 | ||
| val t1: 2 + 2 = 5 // error | ||
| val t2: -1 + 1 = 0 | ||
| val t3: -5 + -5 = -11 // error | ||
|
|
||
| val t4: 10 * 20 = 200 | ||
| val t5: 30 * 10 = 400 // error | ||
| val t6: -10 * 2 = -20 | ||
| val t7: -2 * -2 = 4 | ||
|
|
||
| val t8: 10 / 2 = 5 | ||
| val t9: 11 / -2 = -5 // Integer division | ||
| val t10: 2 / 4 = 2 // error | ||
| val t11: -1 / 0 = 1 // error | ||
|
|
||
| val t12: 10 % 3 = 1 | ||
| val t13: 12 % 2 = 1 // error | ||
| val t14: 1 % -3 = 1 | ||
| val t15: -3 % 0 = 0 // error | ||
|
|
||
| val t16: 1 < 0 = false | ||
| val t17: 0 < 1 = true | ||
| val t18: 10 < 5 = true // error | ||
| val t19: 5 < 10 = false // error | ||
|
|
||
| val t20: 1 <= 0 = false | ||
| val t21: 1 <= 1 = true | ||
| val t22: 10 <= 5 = true // error | ||
| val t23: 5 <= 10 = false // error | ||
|
|
||
| val t24: 1 > 0 = true | ||
| val t25: 0 > 1 = false | ||
| val t26: 10 > 5 = false // error | ||
| val t27: 5 > 10 = true // error | ||
|
|
||
| val t28: 1 >= 1 = true | ||
| val t29: 0 >= 1 = false | ||
| val t30: 10 >= 5 = false // error | ||
| val t31: 5 >= 10 = true // error | ||
|
|
||
| val t32: Abs[0] = 0 | ||
| val t33: Abs[-1] = 1 | ||
| val t34: Abs[-1] = -1 // error | ||
| val t35: Abs[1] = -1 // error | ||
|
|
||
| val t36: Negate[-10] = 10 | ||
| val t37: Negate[10] = -10 | ||
| val t38: Negate[1] = 1 // error | ||
| val t39: Negate[-1] = -1 // error | ||
|
|
||
| val t40: Max[-1, 10] = 10 | ||
| val t41: Max[4, 2] = 4 | ||
| val t42: Max[2, 2] = 1 // error | ||
| val t43: Max[-1, -1] = 0 // error | ||
|
|
||
| val t44: Min[-1, 10] = -1 | ||
| val t45: Min[4, 2] = 2 | ||
| val t46: Min[2, 2] = 1 // error | ||
| val t47: Min[-1, -1] = 0 // error | ||
|
|
||
| val t48: ToString[213] = "213" | ||
| val t49: ToString[-1] = "-1" | ||
| val t50: ToString[0] = "-0" // error | ||
| val t51: ToString[200] = "100" // error | ||
| } |
Oops, something went wrong.
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.
I believe
==and!=should be split as well. Either everything is split according to the supertype or non of it is.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.
I moved them into
scala.compiletime.ops.anyin 8c117c1.The alternative was to duplicate them into each subpackage for each supported type, and then duplicate the constant folding code... which felt like a lot of duplication. Seeing that
==and!=are defined onAny, I think this solution makes the most sense. It also emphasizes that equality is between toAnyvalues, and that1 == "1"will returnfalse.