score:3
this is a method with a single parameter int
named x
, that returns an int
(x + 1) where the return type of int
is inferred by the compiler.
def incimplicit(x: int) = x + 1
this is the same exact method as above, except the return type is explicit, and not inferred.
def incimplicit(x: int): int = x + 1
this is a parameterless method that returns a function1[int, int]
(which is a function with one int
parameter that returns an int
).
def incanonymous = ((x : int) => x + 1)
i.e. the long signature looks like this:
def incanonymous: function1[int, int] = ((x : int) => x + 1)
or
def incanonymous: int => int = ((x : int) => x + 1)
functionally, they are pretty much the same. if an int => int
is required as a parameter, say for def map(f: int => int)
, they will all work. when calling map(incimplicit)
, the first two (which are the same exact method, anyway) will be implicitly converted to functions by eta-expansion. map(incanonymous)
will already have the correct signature, and would require no conversion.
incimplicit(x)
and incanonymous(x)
will always return the same result for any x
. the only difference is that incimplicit
is one method call, and incanonymous
is a method call that returns a function, and then the apply
method of the returned function is invoked. generally, you would not have a method that returns a function unless you had a very good reason to do so.
Source: stackoverflow.com
Related Query
- How are these three approaches of defining a function different?
- Differences between these three ways of defining a function in Scala
- How different are the JavaScript Constructor function and Scala constructor?
- Given any sized Seq[Any], how to call function with this where its parameters are different types, eg Int, String
- How are JavaScript function objects different from Scala function objects?
- How to compare two dataframe and print columns that are different in scala
- Scalaz: how can I accumulate Failures or apply a function to Validations with different types?
- how would monadic rules apply if the function could be of a different type
- Compiler do not recognize function overloading because types are erased. How to overcome this?
- How to combine these three methods into one
- How to ensure that type parameters are different in Scala?
- How are these two higher-order functions different?
- How to combine the same function for different types of argument in scala
- Are these two curry function implementations equal?
- Why do these seemingly equivalent function definitions yield different results?
- How to not apply a function when all parameters are given
- Understanding foldLeft function - how are parameter values typed?
- How is the underscore different from a placeholder in a partially applied function in Scala?
- How to make it so that dependent types in two different traits are recognized as the same type
- How execution of same function defined with "_" is different from the same defined with a named variable in Scala
- How are the columns and rows counted in pascal function in Functional Programming Principles in Scala at coursera?
- How do I make these function definitions D.R.Y.?
- How are these case statements types evaluated?
- How are Function Values processed in Higher Order Methods without definition in Scala?
- How does the col function know which DataFrame we are referring to?
- Why are types of a named function different from an anonymous one in scala
- How to make a function working with different collection type in Scala 2.13?
- How to check if all list elements are different in Scala?
- how do I know if these two versions of scala are exactly the same?
- How to pass different functions fa, fb (different signatures, including returning type) as the same parameter into another function fc?
More Query from same tag
- List of inner classes/objects during compilation
- How to call DataFrameFunctions.createCassandraTable from Java?
- Error while Convert xml to Object or Map in Scala using xstream
- Maven: Failed to load class for data source: com.databricks.spark.csv
- How to access command line parameters in build definition?
- How to create Map using multiple lists in Spark
- Playframework 2.4.x configuration file includes
- Remove duplicates on the combination of Name and Age using Scala and print results, do not use high level API/Framework like pandas /spark-sql etc
- Express SQL IF ELSE in Slick
- adding two defined dataframes in spark scala dataframe for spark API
- Is throwing an Exceptions or returning Option better for "Find by ID" methods?
- Using Proxy Authorization Header with ScalajHTTP
- spark convert spark-SQL to RDD API
- Is it possible to register an UDF that has generic return type in Scala
- Save ml.feature.LabeledPoint to local libsvm file
- In Scala, can I (would some SIP allow me to) specify only certain type parameters of a generic method?
- Testing concurrent/parallel execution
- Typeclass defined with TraversableLike not applying to List
- scala exactly matching a word in a given string
- Statements in scala class
- Validate Scala case class at compile time
- Sonarqube, getting Zero Code Coverage for scala project
- Dataframe to RDD[Row] replacing space with nulls
- case insensitive pattern match in Scala
- Scala: Using right type class instance for reflected/runtime instance
- Spark program behave differently based on --master is set to local[4] or yarn-client
- Dynamic PathMatcher from String
- Serialize linked hash map kryo
- How to implement Map with default operation in Scala
- How to (automatically) inherit settings/tasks from an sbt plugin?