I may be way off here - but would appreciate insight on just how far ..
In the following getFiles
method, we have an anonymous function being passed as an argument.
def getFiles(baseDir: String, filter: (File, String) => Boolean ) = {
val ffilter = new FilenameFilter {
// How to assign to the anonymous function argument 'filter' ?
override def accept(dir: File, name: String): Boolean = filter
}
..
So that override
is quite incorrect: that syntax tries to evaluate
the filter() function which results in a Boolean.
Naturally we could simply evaluate the anonymous function as follows:
override def accept(dir: File, name: String): Boolean = filter(dir, name)
But that approach does not actually replace
the method .
So: how to assign the accept
method to the filter
anonymous function?
Update the error message is
Error:(56, 64) type mismatch;
found : (java.io.File, String) => Boolean
required: Boolean
override def accept(dir: File, name: String): Boolean = filter // { filter(dir, name) }
Another update Thinking more on this - and am going to take a swag : Dynamic languages like python and ruby can handle assignment of class methods to arbitrary functions. But scala requires compilation and thus the methods are actually static. A definitive answer on this hunch would be appreciated.