score:61
This is so-called pass-by-name. It means you are passing a function that should return Int
but is mostly used to implement lazy evaluation of parameters. It is somewhat similar to:
def myFunc(param: () => Int) = param
Here is an example. Consider an answer
function returning some Int
value:
def answer = { println("answer"); 40 }
And two functions, one taking Int
and one taking Int
by-name:
def eagerEval(x: Int) = { println("eager"); x; }
def lazyEval(x: => Int) = { println("lazy"); x; }
Now execute both of them using answer
:
eagerEval(answer + 2)
> answer
> eager
lazyEval(answer + 2)
> lazy
> answer
The first case is obvious: before calling eagerEval()
answer
is evaluated and prints "answer"
string. The second case is much more interesting. We are actually passing a function to lazyEval()
. The lazyEval
first prints "lazy"
and evaluates the x
parameter (actually, calls x
function passed as a parameter).
See also
score:7
To add to Tomasz Nurkiewicz's answer above, the difference I encounter between () => Int and => Int is that the second allows calling with bare blocks:
scala> def myfunc(f : () => Int ) = println("Evaluated: " + f )
myfunc: (f: () => Int)Unit
scala> def myfunc2(f : => Int ) = println("Evaluated: " + f )
myfunc2: (f: => Int)Unit
scala> myfunc({1})
<console>:9: error: type mismatch;
found : Int(1)
required: () => Int
myfunc({1})
^
scala> myfunc2({1})
Evaluated: 1
score:13
Just to make sure there is an answer that uses the proper term: the Scala Language Specification uses the term call-by-name:
The type of a value parameter may be prefixed by =>, e.g. x: => T . The type of such a parameter is then the parameterless method type => T . This indicates that the corresponding argument is not evaluated at the point of function application, but instead is evaluated at each use within the function. That is, the argument is evaluated using call-by-name.
-- Section 4.6.1 of the Scala Language Specification
Source: stackoverflow.com
Related Query
- Function parameter types and =>
- infer a common supertype based on a parameter value and function parameter types
- Scala: when exactly are function parameter types required?
- When does Scala need parameter types for anonymous and expanded functions?
- Function application and types association
- How to get parameter names and types via reflection in Scala/Java methods?
- Scala: Store parameter names and types in a hash map
- Chaining path-dependent types and instantiating them when they having different parameter lists in Scala
- Strange definition and call of a function with curly bracket in Scala (function as parameter of a function)
- forcing argument and return type restrictions on a function based on it's parameterized types
- What is the difference between a generic function and polymorphic argument types in scala?
- Scala - Abstract types and Implicit Parameter Resolution
- Overloading function with call-by-name parameter and function with by-value parameter
- Scala type parameter with multiple types as result of function
- missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) Expected type was:?
- Why does apply with function parameter fail for println and not print?
- Type inference with type aliases and multiple parameter list function
- Scala function literals and unbound placeholder parameter
- Creating a generic reusable function with implicit and types in Scala
- Scala: give a constructor an enumeration and a function that takes that enumeration as a parameter
- Scala defining a function and call it on left side of parameter
- scala: missing parameter type for expanded function: the argument types of an anonymous function must be fully known
- Behaviour of context bounds and implicit parameter lists with regards to path dependent types
- Scala missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5)
- Define Function with Expected Typeclass and Sub-class Type Parameter
- Implementing custom foreach in order to better understand types and function composition
- How to define a method for which the returntype is based on types of argument and type parameter in Scala?
- Scala Function Chain and Types
- Scala method that takes a function as a parameter and then executes it
- pass RDD as parameter and return dataframe to a function - scala
More Query from same tag
- Writing a Java class inside my Scala application
- Can't get path to resource
- Convert java.util.IdentityHashMap to scala.immutable.Map
- How does CanBuildFrom know whether a type can build from another?
- Tell SBT to collect all my dependencies together
- Play framework, scala forms set default value
- How does chisel connect to such a port?
- Spark broadcast big list fail to serialize
- Scala function pointer
- What is the execution order (sequential or concurrent) of futures inside of for comprehension?
- convert java to scala code - change of method signatures
- String interpolator with variable
- How to modify current RequestHeader in playframework2?
- Is Scala for me? (C# developer with focus on functional / OOP style)
- Process Future[Try[Option]] in scala
- Slick 3 - ForeignKeys are not generated
- zipWithIndex for nested collections (release without mutable state)
- Scala Syntax Help Currying
- Using scala spark how to print just the response body value returned from a HTTP post call
- How to return JSON instead of Case Class in akka-Http
- Configure Kamon and AspectjWeaver in Play application
- Scala: How can I match only the first two elements of an arbitrary List
- String similarity with OR condition in MinHash Spark ML
- Implementing traverse function in one iteration in scala
- Apply function to one element of tuple in Scala
- Sbt LibGdx: Build chain hangs in packaging step
- Why joining two spark dataframes fail unless I add ".as('alias)" to both?
- Routing Remote Actors: Peer not authenticated
- How to filter by Optional types in scala spark dataSets
- Disk-persisted-lazy-cacheable-List ™ in Scala