score:0
Named arguments work with function definitions. Parameters with defaults don't need to be given:
class my
{
def f(i:Int = 2, j: Int) = i + j
}
var my = new my()
my.f(j = 1) // i = 2, j = 1
my.f(i = 3, j = 1) // i = 3, j = 1
my.f(3, 1) // i = 3, j = 1
score:1
This only works for methods (which are defined with def
).
def func(i: Int = 2) = i
However, here is somewhat of a "hack" to do it regardless.
score:1
var f(i:Int=2,j:Int) => i + j
This does not work. If you want to define a lambda, try this
val f = (i:Int,j:Int) => i + j
This is the correct approach. Also you cannot assign default values in lambda. You need to define methods with def keyword. Try this:
def f(i:Int, j:Int=2) = i + j
Parameter with default value should be the last parameter because scala compiler will scan values from left-right. It'll only use default values if there are missing ones. In above code f(1)
will produce 3 because j
will use 2 as default value. So use the compulsory arguments in the left side and the ones with default values on right. Hope this helps
Explained here: In Scala, can you make an anonymous function have a default argument?
score:1
You can use a curried function here
def sum1(a:Int=2)(b:Int) = a+b
sum1(33)
Hope this answers your question.
Source: stackoverflow.com
Related Query
- Passing DEFAULTS args in function scala
- Scala Passing Function with Argument
- Scala Implicit parameters by passing a function as argument To feel the adnvatage
- Passing function in Scala
- scala passing function with underscore produces a function not a value
- Scala passing varargs to another function that takes varargs
- Scala Syntax for passing a Class to a function
- Passing type information to function in Scala
- Scala - Passing a function to Future.apply
- Passing a function reference with any arguments to another function in Scala
- Passing data frame as optional function parameter in Scala
- Passing Scala Map as an argument to a function takes too much time
- Filling mutable map with defaults OR alternative to mutable map for Scala function memoization
- scala - Passing a function that takes another function as a parameter
- Parsing command line args and executing a function in scala
- Passing each element of a List to a function in Scala
- Passing method as function in Scala - conversion from Python
- Scala - Passing function as parameter when function is overloaded
- Scala generics - type mismatch error when passing high order function as parameter
- Passing function to overloaded method in Scala
- What is the purpose of passing `Unit` to a function in Scala
- Is it possible in Scala to write an expression that placed in a function parameter, defaults the parameter value?
- Passing a parameterized function in scala
- Scala passing a case class as a function
- I have an array of array and passing it to a function in scala but getting errors
- Calling a function with with args using its reference in Scala
- passing an array reference to a function in scala
- Passing a function to an argument in Scala
- Scala lambda in function args
- scala macro how to convert `HList` to function args
More Query from same tag
- Reading multiple csv files at different folder depths
- How to increase scala stack size?
- unchecked checkbox can't be set to be checked by Jquery or Javascript in scala play framework 2.0.4?
- SPARK : How to create aggregate from RDD[Row] in Scala
- Connecting the first two nodes with an edge from two RDDs in GraphX
- How to log uncaught exceptions in Akka actors?
- Scala filter and print a class
- How to clear state of one notebook without affecting other notebook using command in Azure Databricks?
- sbt assembly error deduplicate
- Add routes/special messages to akka router
- Copy objects in scala but changing "children" for rewriting
- Turn a Spark dataframe Groupby into a sequence of dataframes
- Select rows based on MAX values of a column in ScalaQuery/SLICK
- Apply several transformation functions to string
- Implement timeout in actors
- play framework form validation in scala
- How can provide JsonFormats for case class that references itself?
- Play Framework Json Object mapping partial objects
- Getting an error while passing an expression as a fucntion parameter
- How to get Seq of elements which were grouped in GROUP BY clause?
- curriable function that returns a function in scala via '=>', and (secondly), via 1 arg list followed by another
- How to read scala documentation using reflection
- Scala program to Java 8 program conversion
- Backspace key doesn't work in Scala REPL on macOS
- Akka scheduler : strange behavior in production (messages not firing)
- Scala create List[Int]
- Scala-JS for real web project
- Dynamic compilation of multiple Scala classes at runtime
- Scala remote actors on same machine
- mongodb casbah and list handling