In Scala there are two ways to pass values to function. You can pass it by name or value. I try to explain and show examples depicting this issue by write equivalent java code.
Let's see example class writen in Scala:
class Example {
var invocationCount = 0
def byValue(arg: String) = {
println(arg)
println(arg)
println(arg)
}
def byName(arg: => String) = {
println(arg)
println(arg)
println(arg)
}
def getMessage(): String = {
invocationCount += 1
"invocation count: " + invocationCount
}
}
object Main {
def main(args: Array[String]) {
val example = new Example
println(example.byValue(example.getMessage()))
println(example.byName(example.getMessage()))
}
}
... and output:
invocation count: 1
invocation count: 1
invocation count: 1
()
invocation count: 2
invocation count: 3
invocation count: 4
()
As seen in the first case (method byValue), the method getMessage was evaluated only one time, it result was passed to method byValue and then written three times on screen. And in second case it's a little bit different. In simple terms, when arg was passed by name it is look like passing function as a argument like in JavaScript or like a clousure in Groovy.
What about do that in Java?
Unfortunately it is not possible to do it in such an elegant and readable way in Java, but is possible to simulate. You can do that by using interface.
Let's see Java solution:
// IFunction.java
public interface IFunction< T > {
T call();
}
// Example.java
public class Example {
private int invocationCount;
public void byValue(String arg) {
System.out.println(arg);
System.out.println(arg);
System.out.println(arg);
}
public void byName(IFunction< String > function) {
System.out.println(function.call());
System.out.println(function.call());
System.out.println(function.call());
}
public String getMessage() {
return String.format("invocation count: %d", ++invocationCount);
}
public static void main(String[] args) {
final Example example = new Example();
example.byValue(example.getMessage());
example.byName(new IFunction< String >() {
@Override
public String call() {
return example.getMessage();
};
});
}
}
It look not so pretty as in Scala or Groovy. It remains to wait for Java 8 with Lambda Expressions...
No comments :
Post a Comment