2

Hi I have following code

CookieMock(response, email).cookies: _* 

.cookies is type def cookies: scala.Seq[Cookie]. What does :_* mean in Scala?

Thanks

user_1357
  • 7,430
  • 12
  • 59
  • 99

2 Answers2

3

If you are familiar with Java
here is the same explanation in Java:

varargs

Because * is not a type, you add the underscore.

def printInts(ints: Int*) = ints.mkString(",") 
printInts(1,2,3)
//printInts(List(1,2,3)) //type mismatch; found : List[Int] required: Int
printInts(List(1,2,3): _*) 

paste this to codebrew.io this will clarify.

Community
  • 1
  • 1
JavaSheriff
  • 6,447
  • 17
  • 79
  • 143
0

The : is type ascription. _* is the type you ascribe when you need a Seq[A] to be treated as A*.

http://docs.scala-lang.org/style/types.html

The following are examples of ascription:

Nil: List[String]
Set(values: _*)
"Daniel": AnyRef

Ascription is basically just an up-cast performed at compile-time for the sake of the type checker. Its use is not common, but it does happen on occasion. The most often seen case of ascription is invoking a varargs method with a single Seq parameter. This is done by ascribing the _* type (as in the second example above).

Community
  • 1
  • 1
Chris Martin
  • 29,484
  • 8
  • 71
  • 131
  • What is Type Ascription? Could you give an example with actual values. Let's say we have Seq(1, 2, 3, 4) what would be the result of using :_*? Why would you use it? – user_1357 May 09 '14 at 19:41