Skip to content

Commit 98a92c6

Browse files
authored
Merge pull request #1635 from AndrewZurn/master
Add error message - _* syntax - Parsers.scala:1082
2 parents 0de3797 + 968bee3 commit 98a92c6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,8 @@ object Parsers {
10791079
if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", uscoreStart)
10801080
Typed(t, atPos(uscoreStart) { Ident(tpnme.WILDCARD_STAR) })
10811081
} else {
1082-
syntaxErrorOrIncomplete("`*' expected"); t
1082+
syntaxErrorOrIncomplete(IncorrectRepeatedParameterSyntax())
1083+
t
10831084
}
10841085
case AT if location != Location.InPattern =>
10851086
(t /: annotations())(Annotated)

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ object messages {
580580
}
581581
}
582582

583-
584583
case class ByNameParameterNotSupported()(implicit ctx: Context)
585584
extends Message(21) {
586585
val kind = "Syntax"
@@ -729,4 +728,30 @@ object messages {
729728
| - auxiliary constructors specify the implicit value
730729
|"""
731730
}
731+
732+
case class IncorrectRepeatedParameterSyntax()(implicit ctx: Context) extends Message(27) {
733+
val kind = "Syntax"
734+
val msg = "'*' expected"
735+
val explanation =
736+
hl"""|Expected * in '_*' operator.
737+
|
738+
|The '_*' operator can be used to supply a sequence-based argument
739+
|to a method with a variable-length or repeated parameter. It is used
740+
|to expand the sequence to a variable number of arguments, such that:
741+
|func(args: _*) would expand to func(arg1, arg2 ... argN).
742+
|
743+
|Below is an example of how a method with a variable-length
744+
|parameter can be declared and used.
745+
|
746+
|Squares the arguments of a variable-length parameter:
747+
|${"def square(args: Int*) = args.map(a => a * a)"}
748+
|
749+
|Usage:
750+
|${"square(1, 2, 3) // res0: List[Int] = List(1, 4, 9)"}
751+
|
752+
|Secondary Usage with '_*':
753+
|${"val ints = List(2, 3, 4) // ints: List[Int] = List(2, 3, 4)"}
754+
|${"square(ints: _*) // res1: List[Int] = List(4, 9, 16)"}
755+
|""".stripMargin
756+
}
732757
}

0 commit comments

Comments
 (0)