score:8
in scala functions, whenever you omit a parameter (for a default value), all the following parameters (if provided) are required to be provided with names.
so, if you gave a function like following,
def abc(i1: int, i2: int = 10, i3: int = 20) = i1 + i2 + i3
you can use it in following ways,
abc(1)
abc(1, 2)
abc(1, 2, 3)
but if you want to use default value of i2 and provide a value for i3 then,
abc(1, i3 = 10)
so, in your case, you can use it like following,
val task = task(n = 100)
score:1
the important point here is: if you have a class/method definition that take n
parameters but you need pass to only 1 to n-1
arguments; you use the name of the arguments with =
sign and then the value of the argument you want to pass on, so in your case:
val task = task(n=2)
score:3
if you have task
class defined like this case class task(uuid: string = java.util.uuid.randomuuid().tostring, n: int)
, you may create new instance only with n
argument in such way:
task(n = 1)
Source: stackoverflow.com
Related Query
- scala case class default argument
- default case class argument depending on other arguments scala
- Problem with bounded type parameterised case class and default args in Scala
- How to check which parameters of case class have default value using scala reflection 2.10
- Scala Jackson object mapper setting null instead of default values in case class
- Accept any case class which extends a trait as argument in scala
- Where to put complex logic of Scala case class default value
- How do I write a scala extractor for a case class with default parameters?
- Declaring a method to accept an unknown case class as argument to use it in pattern matching in Scala
- Scala Case Class with Different Argument Types
- Scala case class constructor with WrappedArray argument
- Scala case class with multiple-type argument
- Scala case class with argument type change at runtime
- Scala class constructor's default argument naming
- Scala play-json - doesn't parse/deserialize to single argument case class
- Scala json object as argument for case class
- Scala - Why can't a context bound case class be used as a default constructor parameter of another case class?
- IntelliJ Scala Plugin's case class indentation is absurd
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class inheritance
- Case class to map in Scala
- Extending scala case class without constantly duplicating constructors vals?
- How to convert Row of a Scala DataFrame into case class most efficiently?
- How to get around the Scala case class limit of 22 fields?
- Scala class and case class == comparison
- Scala case match default value
- Scala case class private constructor but public apply method
- Scala case class extending Product with Serializable
- Scala case class update value
- Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class
More Query from same tag
- How to understand this scala code?
- How to add null columns to complex array struct in Spark with a udf
- How to use ReactiveMongo with an enum?
- .pop() equivalent in scala
- Scala: recursion and stack overflow error
- Get maximum from struct with select on all fields Spark dataframe
- How Playframework Scala Facebook or other social media authorization
- Pros and Cons of JAX-RS, Playframework scala (REST backend), Web Api
- run junit as server and inject changes?
- Expected token RPAREN but got Token(STAR,*,180,*) in def isConnected = (ss: Site*) => ??? with Scala 2.11.0-M8
- Apache spark distributed matrix operations?
- Split and filter the column of data frame in Spark
- Relevant Regular Expression in scala
- Deferred type inference on method call
- scala how to drop lines from df based on the column value
- Protobuf: how to describe Option[Timestamp] in proto
- scala pass type parameter function as a parameter of another function
- Dokku: Expose two ports from an application
- Scala: defining a function with parameters and callable
- scalatra issue when running ./sbt: sbt.ResolveException: unresolved dependency: com.typesafe.sbteclipse#sbteclipse-plugin;2.1.1: not found
- Scala 2.10 reflection: Creating an Array from a type
- How EXACTLY is Slick's SimpleLiteral used?
- How to perform subtraction 1 with column value
- How to write the select and case statement together in scala
- How to get the index of the current char while iterating over a string in scala
- Scala's TreeSet vs Java's TreeSet - poll?
- what is the scalaz filterM method?
- Same query on different tables - Scala Slick
- Access Class Variable dynamically in Scala
- Can't access method of companion class from companion object