-
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.
Detail UnapplyInvalidReturnType error message
- Loading branch information
Showing
3 changed files
with
41 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
case class IsIntResult() | ||
|
||
object IsInt: | ||
def unapply(x: Int): IsIntResult = IsIntResult | ||
|
||
@main def test = | ||
val v: String | Int = "Blop" | ||
val res = | ||
v match | ||
case IsInt() => 43 // error: cannot use a product of arity zero as a return type for unapply | ||
// see UnapplyInvalidReturnType in messages.scala | ||
// and https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html#fixed-arity-extractors | ||
case _ => 42 | ||
println(res) |
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,22 @@ | ||
|
||
|
||
class MyProduct extends Product: | ||
def foo = ??? | ||
override def productArity: Int = 1 | ||
override def productElement(n: Int): Any = 42 | ||
override def canEqual(that: Any): Boolean = that.isInstanceOf[MyProduct] | ||
def _1 = 42 | ||
|
||
object MyProductUnapply: | ||
def unapply(x: Int): MyProduct = MyProduct() | ||
|
||
@main def test = | ||
val v: String | Int = "Blop" | ||
val res = | ||
v match | ||
case MyProductUnapply(y) => y // works: a product of arity 1 is accepted as the return type of unapply | ||
// see UnapplyInvalidReturnType in messages.scala | ||
// and https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html#fixed-arity-extractors | ||
case _ => 42 | ||
println(res) | ||
|