File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -1326,7 +1326,7 @@ object Parsers {
1326
1326
if (in.token == YIELD ) { in.nextToken(); ForYield (enums, expr()) }
1327
1327
else if (in.token == DO ) { in.nextToken(); ForDo (enums, expr()) }
1328
1328
else {
1329
- if (! wrappedEnums) syntaxErrorOrIncomplete(" `yield' or `do' expected " )
1329
+ if (! wrappedEnums) syntaxErrorOrIncomplete(YieldOrDoExpectedInForComprehension () )
1330
1330
ForDo (enums, expr())
1331
1331
}
1332
1332
}
Original file line number Diff line number Diff line change @@ -512,4 +512,39 @@ object messages {
512
512
|} """ .stripMargin
513
513
}
514
514
515
+ case class YieldOrDoExpectedInForComprehension ()(implicit ctx : Context ) extends Message (19 ) {
516
+ val kind = " Syntax"
517
+ val msg = hl " ${" yield" } or ${" do" } expected "
518
+
519
+ val code1 = " val numbers = for i <- 1 to 3 yield i"
520
+ val code2 = " val numbers = for (i <- 1 to 3) yield i"
521
+ val code3 = " for (i <- 1 to 3) println(i)"
522
+ val code4 = " for i <- 1 to 3 do println(i) // notice the 'do' keyword"
523
+
524
+ val explanation =
525
+ hl """ When the enumerators in a for comprehension are not placed in parentheses or braces, a ${" do" } or ${" yield" } statement
526
+ |is required after the enumerators section of the comprehension.
527
+ |
528
+ |You can save some keystrokes by omitting the parentheses and writing
529
+ |
530
+ | $code1
531
+ |
532
+ | instead of
533
+ |
534
+ | $code2
535
+ |
536
+ |but the ${" yield" } keyword is still required.
537
+ |
538
+ |For comprehensions that simply perform a side effect without yielding anything can also be written without parentheses
539
+ |but a ${" do" } keyword has to be included. For example,
540
+ |
541
+ | $code3
542
+ |
543
+ | can be written as
544
+ |
545
+ | $code4
546
+ |
547
+ | """ .stripMargin
548
+ }
549
+
515
550
}
You can’t perform that action at this time.
0 commit comments