Difference between function defining

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?

1) def swapOI: ( OIType, NIType) => ValidationNel[ErrorMessage, OIType] =  (oiTypeInstance, niTpeInstance) => {} 
    
2) def swapOI(oiTypeInstance: OIType,  niTpeInstance:NIType): ValidationNel[ErrorMessage, OIType] = { }

The first notation

def swapOI: (OIType, NIType) => ValidationNel[ErrorMessage, OIType] = ???

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.

Anyways, for more differences see this

1 Like

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.

1 Like

Did you checkout the link I posted? For Scala 2 there a lot of differences.

Scala 3 will make both more similar in terms of functionality, leaving syntax and run-time optimizations as the main differences between both.

1 Like

Another good disquisition on methods vs functions is https://tpolecat.github.io/2014/06/09/methods-functions.html

As I always say: this is probably the single most important thing to understand about Scala, during your initial learning.

4 Likes

Sorry, I did not see that there was a link at the end. Going to check it out, thank you!

slides from https://www.slideshare.net/pjschwarz/lambda-expressions-and-java-8-lambda-calculus-lambda-expressions-syntactic-sugar-first-class-functions-second-expedia-tech-know-how-talk-nov-2015

1 Like

Thank you all but
i think one part of question is forgotten(or maybe i can’t understand)
i want to ask it again with new example:

def swapOI: ( String, Int) => ValidationNel[ErrorMessage, String] = ("hi", 23) => {}

why it is not defined in this style:?

def swapOI: ("hi": String, 23:Int) => ValidationNel[ErrorMessage, String] = {}

Well, this:

def swapOI: ( String, Int) => ValidationNel[ErrorMessage, String] =

Is a nullary method (meaning a method with no arguments) which returns a function.

This:

("hi", 23) => {}

Is not valid syntax, but I guess you wanted to show a function literal, like:

(name, age) => { ... }

So this can be used like:

swapIO("hi", 23)

Which basically desugars to

(swapIO).apply("hi", 23)

So it calls the method which then returns a function and then calls that function.

Why it is not defined in this style?

Again, this:

def swapOI: ("hi": String, 23:Int) => ValidationNel[ErrorMessage, String] = {}

Is not valid syntax.
Maybe you meant something like:

def swapOI(name: String, age:Int): ValidationNel[ErrorMessage, String] = {
  ...
}

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)

2 Likes