score:46
def someMethod(values : Int*)
Gives an array. Put the variable argument parameter as the last formal parameter.
score:50
Both Java and Scala have varargs, and both only support it for the last parameters.
def varargTest(ints:Int*) { ints.foreach(println) }
From this article, the difference is in the type used for the varargs arguments:
- array for Java
- Seq (Sequence) for Scala: it can be iterated and many methods such as collections foreach, map, filter, find, ... are available
The '*' stands for 0 or more arguments.
Note: if the parameter values are already "packaged" as a sequence, such as a list, it fails:
# varargTest(List(1,2,3,4,5))
# //--> error: type mismatch;
# //--> found : List[Int]
# //--> required: Int
# //--> varargTest(List(1,2,3,4,5))
# //-->
But this will pass:
varargTest(List(1,2,3):_*)
//--> 1
//--> 2
//--> 3
'_
' is a placeholder shortcut for type inference.
'_*
' is here applyied to a 'repeated type.
Section 4.6.2 of Scala Specification mentions:
The last value parameter of a parameter section may be suffixed by “
*”
, e.g.(..., x:T *)
.
The type of such a repeated parameter inside the method is then the sequence typescala.Seq[T]
.
Methods with repeated parametersT*
take a variable number of arguments of typeT
.
(T1, . . . , Tn,S*)U => (T1, . . . , Tn,S, . . . , S)U,
The only exception to this rule is if the last argument is marked to be a sequence argument via a
_*
type annotation.
(e1, . . . , en,e0: _*) => (T1, . . . , Tn, scala.Seq[S]).
Note bis: beware of the underlying type erasure of Java:
//--> error: double definition:
//--> method varargTest:(ints: Seq[Int])Unit and
//--> method varargTest:(ints: Int*)Unit at line 10
//--> have same type after erasure: (ints: Sequence)Unit
//--> def varargTest(ints:Seq[Int]) { varargTest(ints: _*) }
Source: stackoverflow.com
Related Query
- How can Scala receive multiple parameters in a method definition?
- How can I make a scala method parameter type that is a collection of multiple types that can be converted to a given type?
- How to create a Source that can receive elements later via a method call?
- How do you create scala anonymous function with multiple implicit parameters
- How to declare scala method so that it can be called from Java using varargs style
- How can I manage multiple versions of Scala & SBT in my dev environment?
- How can I reflectively call a method on a Scala object from Java?
- scala coalesces multiple function call parameters into a Tuple -- can this be disabled?
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- Multiple type parameters on a scala method
- How can I make multiple parameters in an anonymous function implicit?
- How does Scala know what method to invoke ( named parameters )
- How can I test a Subscriber's receive method in Akka Cluster?
- How can you force Twitter's Scala Eval to prevent injecting code? I would like to limit Eval to specifying config parameters
- In Scala, how can I restrict multiple parameters must be a type inside the same type?
- How can I efficiently run a scala program multiple times?
- How can I overload functions with generic parameters in scala
- In the scala spray framework, how can I create multiple http clients that have different configurations (e.g., timeouts, retries)
- How can I call a method defined on a Scala package object from Java? (Scala 2.10.x)
- How to get match type with multiple type parameters to work correctly in Scala 3
- Can I use a type bound on a Scala abstract method and then "tighten up" the definition in a subclass?
- Scala method definition named parameters vs. unnamed
- Calling Java method that receive variable amount of parameters from Scala
- How to Merge values from multiple rows so they can be processed together - Spark scala
- How can I create a factory method in scala that returns a class whose generic parameter is constrained to be a ClassManifest?
- How can I read multiple parquet files in spark scala
- How Nothing can be subclass of all types when multiple inheritance is not supported in scala
- How can I prevent an Scala function to receive 'null' as an argument
- How to inject parameters into a class/trait method in Scala
- How can I check that method parameters have the same type as the methods this?
More Query from same tag
- how do i convert a flat dataframe into a nested json in spark (scala or java)
- No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]
- Play2 and Scala (Jerkson) - List of JsObjects - how to convert to a Json object
- Migration-Manager / binary compatibility: overriding hash-code with reference to private[this]
- Creating an Akka fat Jar
- How to compose routes in actor's receive with runRoute?
- Declare a generic class in scala without square brackets
- Why is my functionality changing by moving some logic?
- How to support marshalling and unmarshalling in Akka HTTP with JSON Spray for HashMap fields?
- Scala Compilation Error on Vert.x
- Can you use 2 java nio file systems in one program?
- Scala Option[(A, B)] pattern matching
- scala list match
- Why is scala.xml.Atom type-parameterized?
- In Spark,while writing dataset into database it takes some pre-assumed time for save operation
- How do I flatten an Array[Future[Seq[T]]]
- scaladoc is not compiling like the compiler
- Call a REST API of a module controller in a play application
- Dividing a DafaFrame in spark to multiple DataFrames and writing to directories
- why doesn't scala recognize com.vaadin.ui.MenuBar.MenuItem
- How to use Typesafe's Config in Scala with encrypted passwords
- How to submit Spark SQL application with parameter used in SQL statements?
- play/scala , implicit request => what is meaning?
- Scala Seq GroupBy with Future
- Use method in sql interpolator
- Scala exception signature definition using generics
- Scala/Play framework parameter name with a dot in it
- map().partitionBy() How many shuffles these 2 commands will do?
- Global.onStop() not getting called on reload in dev mode
- Scala view bounds, what's wrong with my example