|
2 | 2 | 3 | val s(): String = "hello, world" // error
|
3 | 3 | | ^
|
4 | 4 | | no pattern match extractor named s was found
|
5 |
| - | |
6 |
| - | longer explanation available when compiling with `-explain` |
| 5 | + |--------------------------------------------------------------------------------------------------------------------- |
| 6 | + | Explanation (enabled by `-explain`) |
| 7 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 8 | + | An application s(...) in a pattern can refer to an extractor |
| 9 | + | which defines an unapply or unapplySeq method. Case classes and enum cases |
| 10 | + | implicitly define extractors with the name of the class or enum case. |
| 11 | + | Here, no extractor named s was found, so the pattern could not be typed. |
| 12 | + --------------------------------------------------------------------------------------------------------------------- |
7 | 13 | -- [E189] Not Found Error: tests/neg/i18684.scala:5:6 ------------------------------------------------------------------
|
8 | 14 | 5 | val i() = 22 // error
|
9 | 15 | | ^
|
10 | 16 | | no pattern match extractor named i was found
|
11 |
| - | |
12 |
| - | longer explanation available when compiling with `-explain` |
| 17 | + |--------------------------------------------------------------------------------------------------------------------- |
| 18 | + | Explanation (enabled by `-explain`) |
| 19 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 20 | + | An application i(...) in a pattern can refer to an extractor |
| 21 | + | which defines an unapply or unapplySeq method. Case classes and enum cases |
| 22 | + | implicitly define extractors with the name of the class or enum case. |
| 23 | + | Here, no extractor named i was found, so the pattern could not be typed. |
| 24 | + --------------------------------------------------------------------------------------------------------------------- |
13 | 25 | -- [E189] Not Found Error: tests/neg/i18684.scala:10:8 -----------------------------------------------------------------
|
14 | 26 | 10 | val foo() = "33" // error
|
15 | 27 | | ^^^
|
16 | 28 | | no pattern match extractor named foo was found
|
17 |
| - | |
18 |
| - | longer explanation available when compiling with `-explain` |
| 29 | + |-------------------------------------------------------------------------------------------------------------------- |
| 30 | + | Explanation (enabled by `-explain`) |
| 31 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 32 | + | An application foo(...) in a pattern can refer to an extractor |
| 33 | + | which defines an unapply or unapplySeq method. Case classes and enum cases |
| 34 | + | implicitly define extractors with the name of the class or enum case. |
| 35 | + | Here, no extractor named foo was found, so the pattern could not be typed. |
| 36 | + -------------------------------------------------------------------------------------------------------------------- |
19 | 37 | -- [E127] Pattern Match Error: tests/neg/i18684.scala:12:6 -------------------------------------------------------------
|
20 | 38 | 12 | val inner(x) = 3 // error
|
21 | 39 | | ^^^^^
|
22 | 40 | | Test.inner cannot be used as an extractor in a pattern because it lacks an unapply or unapplySeq method
|
| 41 | + |-------------------------------------------------------------------------------------------------------------------- |
| 42 | + | Explanation (enabled by `-explain`) |
| 43 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 44 | + | An unapply method should be defined in an object as follow: |
| 45 | + | - If it is just a test, return a Boolean. For example case even() |
| 46 | + | - If it returns a single sub-value of type T, return an Option[T] |
| 47 | + | - If it returns several sub-values T1,...,Tn, group them in an optional tuple Option[(T1,...,Tn)] |
23 | 48 | |
|
24 |
| - | longer explanation available when compiling with `-explain` |
| 49 | + | Sometimes, the number of sub-values isn't fixed and we would like to return a sequence. |
| 50 | + | For this reason, you can also define patterns through unapplySeq which returns Option[Seq[T]]. |
| 51 | + | This mechanism is used for instance in pattern case List(x1, ..., xn) |
| 52 | + -------------------------------------------------------------------------------------------------------------------- |
0 commit comments