-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11904 from dotty-staging/fix-10930-alt
Introduce `asMatchable` escape hatch
- Loading branch information
Showing
6 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,13 @@ | ||
import language.future | ||
@main def Test = | ||
type LeafElem[X] = X match | ||
case String => Char | ||
case Array[t] => LeafElem[t] | ||
case Iterable[t] => LeafElem[t] | ||
case AnyVal => X | ||
|
||
def leafElem[X](x: X): LeafElem[X] = x match | ||
case x: String => x.charAt(0) // error | ||
case x: Array[t] => leafElem(x(1)) // error | ||
case x: Iterable[t] => leafElem(x.head) // error | ||
case x: AnyVal => x // error |
This file contains 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,33 @@ | ||
import language.future | ||
import compiletime.asMatchable | ||
|
||
@main def Test = | ||
type LeafElem[X] = X match | ||
case String => Char | ||
case Array[t] => LeafElem[t] | ||
case Iterable[t] => LeafElem[t] | ||
case AnyVal => X | ||
|
||
def leafElem[X](x: X): LeafElem[X] = x.asMatchable match | ||
case x: String => x.charAt(0) | ||
case x: Array[t] => leafElem(x(1)) | ||
case x: Iterable[t] => leafElem(x.head) | ||
case x: AnyVal => x | ||
|
||
def f[X](x: X) = x | ||
|
||
def leafElem2[X](x: X): LeafElem[X] = f(x).asMatchable match | ||
case x: String => x.charAt(0) | ||
case x: Array[t] => leafElem(x(1)) | ||
case x: Iterable[t] => leafElem(x.head) | ||
case x: AnyVal => x | ||
|
||
val x1: Char = leafElem("a") | ||
assert(x1 == 'a') | ||
val x2: Char = leafElem(Array("a", "b")) | ||
assert(x2 == 'b') | ||
val x3: Char = leafElem(List(Array("a", "b"), Array(""))) | ||
assert(x3 == 'b') | ||
val x4: Int = leafElem(3) | ||
assert(x4 == 3) | ||
|