Does anyone know the type of => Unit in scala? I don’t know the meaning of => Unit and how to use it. I defined a function like below:
def test(code: => Unit){
print("start ...")
code
print("end ....")
}
test(print(1))
Does it means a function with any arguments returning Unit?
Thanks
This kind of parameter are called by-name parameter
=> Brepresents a block a of code which return aBvalue, their purpose is that they are evaluated only when you call the parameter.And you can call
fooin the following way :or
The other style of passing parameter is by-value : parameters are evaluated before they are sent to the method
References
Extract from the Book Functionnal Programming in Scala
More differences between by-name parameter and by-value parameter illustrated