Skip to content

Commit 8937d0b

Browse files
oderskyWojciechMazur
authored andcommitted
Add example to explanation
[Cherry-picked eed38ec]
1 parent 9c0bca3 commit 8937d0b

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,8 +2357,18 @@ class ExtractorNotFound(val name: Name)(using Context) extends NotFoundMsg(Extra
23572357
def msg(using Context) = i"no pattern match extractor named $name was found"
23582358
def explain(using Context) =
23592359
i"""An application $name(...) in a pattern can refer to an extractor
2360-
|which defines an unapply or unapplySeq method. Case classes and enum cases
2361-
|implicitly define extractors with the name of the class or enum case.
2360+
|which defines an unapply or unapplySeq method. Example:
2361+
|
2362+
| object split:
2363+
| def unapply(x: String) =
2364+
| val (leading, trailing) = x.splitAt(x.length / 2)
2365+
| Some((leading, trailing))
2366+
|
2367+
| val split(fst, snd) = "HiHo"
2368+
|
2369+
|The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and
2370+
|`snd` as the second half "Ho" of the right hand side "HiHo". Case classes and
2371+
|enum cases implicitly define extractors with the name of the class or enum case.
23622372
|Here, no extractor named $name was found, so the pattern could not be typed."""
23632373

23642374
class MemberWithSameNameAsStatic()(using Context)

tests/neg/i18684.check

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66
| Explanation (enabled by `-explain`)
77
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88
| 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.
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.
1121
| Here, no extractor named s was found, so the pattern could not be typed.
1222
---------------------------------------------------------------------------------------------------------------------
1323
-- [E189] Not Found Error: tests/neg/i18684.scala:5:6 ------------------------------------------------------------------
@@ -18,8 +28,18 @@
1828
| Explanation (enabled by `-explain`)
1929
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2030
| 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.
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.
2343
| Here, no extractor named i was found, so the pattern could not be typed.
2444
---------------------------------------------------------------------------------------------------------------------
2545
-- [E189] Not Found Error: tests/neg/i18684.scala:10:8 -----------------------------------------------------------------
@@ -30,8 +50,18 @@
3050
| Explanation (enabled by `-explain`)
3151
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3252
| 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.
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.
3565
| Here, no extractor named foo was found, so the pattern could not be typed.
3666
--------------------------------------------------------------------------------------------------------------------
3767
-- [E127] Pattern Match Error: tests/neg/i18684.scala:12:6 -------------------------------------------------------------

0 commit comments

Comments
 (0)