2

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.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • The original code does not explain to the computer how to apply the filter. You have to evaluate the filtering function inside the accept method. There are two arguments to your filter function. `override def accept(dir: File, name: String): Boolean = filter(dir, name)` should do it. – Bob Dalgleish Aug 01 '15 at 23:55
  • @BobDalgleish please see the OP : i had added clarification on that before your comment appeared but probably after you saw the original post. – WestCoastProjects Aug 01 '15 at 23:56
  • @Paul added the error message to the OP – WestCoastProjects Aug 01 '15 at 23:58
  • Did you try `override val accept = filter`? That's the only way I know to "assign" a method value. – Bob Dalgleish Aug 02 '15 at 00:00
  • @BobDalgleish I will try that - looks promising ! – WestCoastProjects Aug 02 '15 at 00:01
  • @BobDalgleish No dice: `value accept overrides nothing`. Presumably since the base class is a `def` not a `val` ? – WestCoastProjects Aug 02 '15 at 00:03
  • Do you need braces around `filter` on the RHS so that it doesn't get called immediately? See also [here](http://stackoverflow.com/questions/6349202/can-i-pass-an-arbitrary-function-to-another-function-in-scala) where braces are used. – Paul Aug 02 '15 at 00:04
  • @Paul That gives same error as at bottom of OP – WestCoastProjects Aug 02 '15 at 00:09

2 Answers2

1

There is no way easy or type-safe way (that I know of) to assign a function to a method as they are different types. In Python or JavaScript you could do something like this:

var fnf = new FilenameFilter();
fnf.accepts = filter;

But in Scala you have to do the delegation:

val fnf = new FilenameFilter {
   override def accept(dir: File, name: String): Boolean = filter(dir, name) 
 }
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
0

I think you are actually misunderstanding the meaning of the override keyword. It is for redefining methods defined in the superclass of a class, not redefining a method in an instance of a class.

If you want to redefine the accept method in the instances of FilenameFilter, you will need to add the filter method to the constructor of the class, like:

class FilenameFilter(val filter: (File, String) => Boolean) {
  def accept(dir: File, name: String): Boolean = filter(dir, name)
}
Attila Repasi
  • 1,803
  • 10
  • 11
  • Oh i'm well aware of override meaning. The surprise here is that *not including* the `override` actually compiles at all. I would have anticipated that we need to do so in order for the anonymous FilenameFilter to be properly implemented. – WestCoastProjects Aug 02 '15 at 03:09
  • Override is optional when redefining a method of the superclass, because it only signals to the compiler to check for the definition of the method in the superclass, and throw a compile error, when it does not exist or has a different signature. – Attila Repasi Aug 02 '15 at 07:15