Trivial SAM convertion
brian.goetz at oracle.com
Tue Apr 2 06:22:00 PDT 2013
> During my attempts to *lambdafying* my code, I came to places like this:
> *Predicate<File> isDir = File::isDirectory;*
> *Predicate<File> isFile = isDir.negate();*
> FileFilter isFileFilter = isFile::test;
> *At first, naively I just wrote:
> *FileFilter isFileFilter = isFile;
> *After realizing my mistake, I wondered 'Why not *?' If we have two SAM
> interfaces with identical signatures (test & accept) then one can be
> converted to other.
Yes, the EG discussed the issue of "SAM to SAM conversion" at length.
The deciding factor was that an existing language feature already gives
you most of what you want:
FilterFilter isFileFilter = isFile::test;
Going farther did not seem worthwhile. The alternatives were:
- a special syntax for the conversion
- a silent magic conversion
Given that the above is not syntactically awful, and nicely explicit,
prudence dictated that we stop there.
> If I may, one more question. Is this will be ever a valid code ?:
> Predicate<File> isFile = (File::isDirectory).negate();
Doing so would require unbounded type analysis. To solve this, the
compiler would have to say: "What are the instantiations of types that
have a negate() method that returns Predicate<File>, and, if there's
exactly one, can I use that as a target type for File::isDirectory?"
Seems a lot to ask the compiler.
More information about the lambda-dev