Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,8 @@ object Parsers {
if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", uscoreStart)
Typed(t, atPos(uscoreStart) { Ident(tpnme.WILDCARD_STAR) })
} else {
syntaxErrorOrIncomplete("`*' expected"); t
syntaxErrorOrIncomplete(IncorrectRepeatedParameterSyntax())
t
}
case AT if location != Location.InPattern =>
(t /: annotations())(Annotated)
Expand Down
27 changes: 26 additions & 1 deletion src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ object messages {
}
}


case class ByNameParameterNotSupported()(implicit ctx: Context)
extends Message(21) {
val kind = "Syntax"
Expand Down Expand Up @@ -729,4 +728,30 @@ object messages {
| - auxiliary constructors specify the implicit value
|"""
}

case class IncorrectRepeatedParameterSyntax()(implicit ctx: Context) extends Message(27) {
val kind = "Syntax"
val msg = "'*' expected"
val explanation =
hl"""|Expected * in '_*' operator.
|
|The '_*' operator can be used to supply a sequence-based argument
|to a method with a variable-length or repeated parameter. It is used
|to expand the sequence to a variable number of arguments, such that:
|func(args: _*) would expand to func(arg1, arg2 ... argN).
|
|Below is an example of how a method with a variable-length
|parameter can be declared and used.
|
|Squares the arguments of a variable-length parameter:
|${"def square(args: Int*) = args.map(a => a * a)"}
|
|Usage:
|${"square(1, 2, 3) // res0: List[Int] = List(1, 4, 9)"}
|
|Secondary Usage with '_*':
|${"val ints = List(2, 3, 4) // ints: List[Int] = List(2, 3, 4)"}
|${"square(ints: _*) // res1: List[Int] = List(4, 9, 16)"}
|""".stripMargin
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also happen in:

case class Foo(i: Int*)

(null: Foo) match {
  case Foo(i: _) => // ...
}

And providing func with arguments could also be done like: func(1, 2, 3) right? Basically I think your explanation is good, but try to explain what the _* construct does :)

}