what is difference between these two definition:
the second definition is standard definition but what is the purpose of first definition?
is it a pattern? when i should use it?
defines a function which return type is a function that takes an OIType as its first and a NIType as it second argument and returns a ValidationNel[ErrorMessage, OIType].
Example:
type ValidationNel[A, B] = Either[A, B]
def swapOI: (String, String) => ValidationNel[String, String] = (a: String, b: String) => {
if (a == null) {
Left("'a' is null!")
} else {
Right(a + b)
}
}
val fun = swapOI
val res = fun("a", "b")
res match {
case Left(value) => println(s"Error: $value")
case Right(value) => println(s"a + b: $value")
}
The second definition actually defines a function that could be returned by the first.
Minor correction a def creates a method not a function. So the first one is a method that always returns the same function, which is kind of unneeded indirection.
Is there in Scala really a strong distinction between method and function? I know people tend to differenciate between procedure and function, but I did not know that this is true for method, too.
A method and a function are two very different things in Scala.
Unlike in many other languages, where a function is a callable piece of code that returns a value, in Scala, a function is an object that extends one of the FunctionN traits, which implies it has an apply method.
Then, for what we can tell, there is no real reason for not doing that.
Both have the same behavior and look alike when calling them.
(However a method that always returns the same function is slower than just a method that returns a value, and is harder to read; So, I would never do that)