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