Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call by name parameters in scala functions

Tags:

scala

I have a question concerning the difference between these two functions:

def getFunction(checkpointPath: String,
                  sparkConf: SparkConf,
                  creatingFunc: () => StreamingContext): StreamingContext = {
    function body
  }


def getFunction(checkpointPath: String,
                  sparkConf: SparkConf,
                  creatingFunc:  => StreamingContext): StreamingContext = {
    function body
  }

so the called by name param is the same:

creatingFunc:  => StreamingContext 

and

creatingFunc: () => StreamingContext

or no ?

like image 804
scalacode Avatar asked Nov 22 '25 10:11

scalacode


1 Answers

The two are not the same. The first case specifies method parameter that is call-by-name

creatingFunc:  => StreamingContext 

whilst the second case specifies pass-by-value method parameter where the parameter happens to be a function of type () => StreamingContex

creatingFunc: () => StreamingContext

For example, consider the following two methods

def foo(arg: () => String) = ""
def foo(arg: => String) = ""

then

foo(() => "") // resolves to call first foo
foo("")       // resolves to call second foo
like image 137
Mario Galic Avatar answered Nov 24 '25 22:11

Mario Galic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!