Back2Basics: Pass-By-Name and HOF in Scala argue as a Husband And Wife.

Table of contents
Reading Time: 2 minutes

Scala has lots of features and one of the features always confused me called “pass-by-name” argument.  For me pass by name is same as “higher-order-function” but different from just syntax otherwise everything is same. While googling out for their difference, I found:

  1. Pass-by-name is lazy evaluation but higher-order-functions are eager.
  2. Pass-by-name is used for constructs custom controls.

and maybe there are more, but these two are available everywhere. Every time, I read about its different but always have one doubt Why pass-by-name is required ???

Mustaches-Baby-Crying-Sad-Face-Funny-Image

Finally, I found my answer and the main differences, which satisfy my internal soul and clear all doubts about “pass-by-name” and “higher-order-function“.

pass-by-name

With some findings, the conclusion is like “pass-by-name” act as a husband but “higher-order-functions” act as a wife. How??

We are sure, wifes are always happy and believed in arguments but husbands are like innocent creature which accepts every argument and always evaluate lazily when someone tells. The same theory is implemented in Scala.

2-300x168

In scala, “higher-order-functions” are same as wife, while we declaring “higher-order-function” as an argument, it only works if passed parameter type and a count is match but “pass-by-name” is like of innocent husband which accepts any type of arguments and any number of arguments and only evaluate if someone ask otherwise nothing can do. Let’s take a Scala example:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


def method1(arg: Int): String = {
s"The method1 passed value is $arg"
}
def method2(arg1: Int, arg2: Int): String = {
s"The method2 passed value sum is ${arg1 + arg2}"
}
def method3(arg1: Int, arg2: Int, arg3: Int): String = {
s"The method3 passed value sum is ${arg1 + arg2 + arg3}"
}
def method4(string: String): String = {
s"String length is ${ string.length }"
}
def hofArg1(f: Int => String) = {
println(f(1))
}
def hofArg2(f: (Int, Int) => String) = {
println(f(1, 1))
}
def hofArg3(f: (Int, Int, Int) => String) = {
println(f(1, 1, 1))
}
// higher order functions
hofArg1(method1)
hofArg2(method2)
hofArg3(method3)
// hofArg1(method4) not compiles because function type nu match
// pass by name
def byNameArg(methodValue: => String) = {
println(methodValue)
}
byNameArg(method1(1))
byNameArg(method2(1, 1))
byNameArg(method3(1, 1, 1))
byNameArg(method4("Hello Name"))
  • The example contains four methods with the different number of arguments and different type of arguments “method(x)“.
  • While we need to pass “method(x)” as a “higher-order-functions“, we require to create different methods according to passed arguments, so we have three “hofArg(x)” methods. 
  • But in the case of “pass by name” argument, we are creating only one method which takes an argument as “pass-by-name” and method called “byNameArg“. 
  • While calling this method with different types and number of arguments, the method executes successfully and prints its results.

After this huge different now I am pretty much sure, when we need to use “pass by name” and when to use “higher-order-function“.


knoldus-advt-sticker


 

Written by 

Harmeet Singh is a lead consultant, with experience of more than 5 years. He has expertise in Scala, Java, JVM, and functional programming. On a personal front; he is a food lover.

2 thoughts on “Back2Basics: Pass-By-Name and HOF in Scala argue as a Husband And Wife.2 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading