|
| 1 | +-- [E189] Not Found Error: tests/neg/i18684.scala:3:6 ------------------------------------------------------------------ |
| 2 | +3 | val s(): String = "hello, world" // error |
| 3 | + | ^ |
| 4 | + | no pattern match extractor named s was found |
| 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. Example: |
| 10 | + | |
| 11 | + | object split: |
| 12 | + | def unapply(x: String) = |
| 13 | + | val (leading, trailing) = x.splitAt(x.length / 2) |
| 14 | + | Some((leading, trailing)) |
| 15 | + | |
| 16 | + | val split(fst, snd) = "HiHo" |
| 17 | + | |
| 18 | + | The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and |
| 19 | + | `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and |
| 20 | + | enum cases implicitly define extractors with the name of the class or enum case. |
| 21 | + | Here, no extractor named s was found, so the pattern could not be typed. |
| 22 | + --------------------------------------------------------------------------------------------------------------------- |
| 23 | +-- [E189] Not Found Error: tests/neg/i18684.scala:5:6 ------------------------------------------------------------------ |
| 24 | +5 | val i() = 22 // error |
| 25 | + | ^ |
| 26 | + | no pattern match extractor named i was found |
| 27 | + |--------------------------------------------------------------------------------------------------------------------- |
| 28 | + | Explanation (enabled by `-explain`) |
| 29 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 30 | + | An application i(...) in a pattern can refer to an extractor |
| 31 | + | which defines an unapply or unapplySeq method. Example: |
| 32 | + | |
| 33 | + | object split: |
| 34 | + | def unapply(x: String) = |
| 35 | + | val (leading, trailing) = x.splitAt(x.length / 2) |
| 36 | + | Some((leading, trailing)) |
| 37 | + | |
| 38 | + | val split(fst, snd) = "HiHo" |
| 39 | + | |
| 40 | + | The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and |
| 41 | + | `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and |
| 42 | + | enum cases implicitly define extractors with the name of the class or enum case. |
| 43 | + | Here, no extractor named i was found, so the pattern could not be typed. |
| 44 | + --------------------------------------------------------------------------------------------------------------------- |
| 45 | +-- [E189] Not Found Error: tests/neg/i18684.scala:10:8 ----------------------------------------------------------------- |
| 46 | +10 | val foo() = "33" // error |
| 47 | + | ^^^ |
| 48 | + | no pattern match extractor named foo was found |
| 49 | + |-------------------------------------------------------------------------------------------------------------------- |
| 50 | + | Explanation (enabled by `-explain`) |
| 51 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 52 | + | An application foo(...) in a pattern can refer to an extractor |
| 53 | + | which defines an unapply or unapplySeq method. Example: |
| 54 | + | |
| 55 | + | object split: |
| 56 | + | def unapply(x: String) = |
| 57 | + | val (leading, trailing) = x.splitAt(x.length / 2) |
| 58 | + | Some((leading, trailing)) |
| 59 | + | |
| 60 | + | val split(fst, snd) = "HiHo" |
| 61 | + | |
| 62 | + | The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and |
| 63 | + | `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and |
| 64 | + | enum cases implicitly define extractors with the name of the class or enum case. |
| 65 | + | Here, no extractor named foo was found, so the pattern could not be typed. |
| 66 | + -------------------------------------------------------------------------------------------------------------------- |
| 67 | +-- [E127] Pattern Match Error: tests/neg/i18684.scala:12:6 ------------------------------------------------------------- |
| 68 | +12 | val inner(x) = 3 // error |
| 69 | + | ^^^^^ |
| 70 | + | Test.inner cannot be used as an extractor in a pattern because it lacks an unapply or unapplySeq method |
| 71 | + |-------------------------------------------------------------------------------------------------------------------- |
| 72 | + | Explanation (enabled by `-explain`) |
| 73 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 74 | + | An unapply method should be defined in an object as follow: |
| 75 | + | - If it is just a test, return a Boolean. For example case even() |
| 76 | + | - If it returns a single sub-value of type T, return an Option[T] |
| 77 | + | - If it returns several sub-values T1,...,Tn, group them in an optional tuple Option[(T1,...,Tn)] |
| 78 | + | |
| 79 | + | Sometimes, the number of sub-values isn't fixed and we would like to return a sequence. |
| 80 | + | For this reason, you can also define patterns through unapplySeq which returns Option[Seq[T]]. |
| 81 | + | This mechanism is used for instance in pattern case List(x1, ..., xn) |
| 82 | + -------------------------------------------------------------------------------------------------------------------- |
0 commit comments