Here at work most people use Java, while I am working with Scala. We have decided to gather some common classes in an library which will be written in Java. Now I want to add some pseudo-functional programming to the library, take a look at the following:
java:
public interface Func<A, R> {
public R f(a A);
}
public AClass {
public <R> ArrayList<R> myMethod(
Func<String, R> func
) {
// ...
}
}
usage in java:
AClass ac = new AClass();
ArrayList<String> al = ac.myMethod(
new Func<String, String> (
public String f(String s) {
return s + "!";
}
})
The above is not exactly exiting (more like daunting actually, from a scala perspective). Is there any way to summon some scala magic to be able to do something like the following in scala:
var ac = new ACLass
var al = ac.myMethod(str => str + "!") // alternative 1
al = ac.myMethod { case: str:String => str + "!" } // alternative 2
I messed around with implicits for a while but couldn't get things sorted out =P.