I am new to Scala, and have seen many ways to define a function but could not find a clear explanation on the differences, and when to use which form.
What are the main differences between the following function definitions?
With '='
def func1(node: scala.xml.Node) = { print(node.label + " = " + node.text + ",") }
Without '='
def func2 (node: scala.xml.Node) { print(node.label + " = " + node.text + ",") }
With '=>'
def func3 = (node: scala.xml.Node) => { print(node.label + " = " + node.text + ",") }
As a var
var func4 = (node: scala.xml.Node) => { print(node.label + " = " + node.text + ",") }
Without a block
def func5 (node: scala.xml.Node) = print(node.label + " = " + node.text + ",")
They all seem to compile and render the same result when used as a callback for
xmlNodes.iterator.foreach(...)
- Is there any difference in the bytecode each generate?
- Are there any guidlines when to use which form?