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.

8
  • 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. Commented Aug 1, 2015 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. Commented Aug 1, 2015 at 23:56
  • @Paul added the error message to the OP Commented Aug 1, 2015 at 23:58
  • Did you try override val accept = filter? That's the only way I know to "assign" a method value. Commented Aug 2, 2015 at 0:00
  • @BobDalgleish I will try that - looks promising ! Commented Aug 2, 2015 at 0:01

2 Answers 2

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) 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

OK I'll accept this. I had been doing python recently so got into other habits.
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)
}

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.