The lambda matches the FunctionalInterface signature, but no. How do you explain that the argument is passed at all?

I am currently working on this project . This works surprisingly well.

However, after reading README again, I began to think about how to document something that was pushing me ...

To give an example and forgetting for a moment that exceptions can be thrown, he reads:

Files.list(somePath).map(Path::toRealPath).forEach(System.out::println) 

OK Now the Path method is associated with this . Of course, we do not pass LinkOption .

Again: let's forget for a moment that it raises any exception.

The .map() stream takes a Function value as an argument. This interface for Function<T, R> is defined as:

 R apply(T t); 

But the method I use takes no arguments. At first glance, it doesn't seem to match Function , right? Moreover...

It can be written as:

 path -> path.toRealPath() 

It seems that the mechanism used is somewhat capable of invoking a method in a "stream object" if the method reference has no argument or something like this ...

I would like to document this, and I missed something here.

What am I missing?

+5
source share
1 answer

Non-static methods have a receiver object ( this ) as an implicit first argument. Consequently, Class::nonStaticMethod has one more argument that might be expected.

Java Language Specification Section 15.13.1, Declaration of Compilation Time Method Reference :

  • Secondly, for a given type of function with n parameters, a set of potentially applicable methods is determined:

    • If the method reference expression is of the form ReferenceType :: [TypeArguments] Identifier, potentially applicable methods are member methods of the search type that have the appropriate name (specified by the identifier), availability, arity (n or n-1) and specify the arty argument (obtained from [TypeArguments]), as indicated in ยง15.12.2.1.

      Two different categories are considered: n and n-1, taking into account the possibility that this form refers to the static method or to the instance method.

+3
source

Source: https://habr.com/ru/post/1209816/


All Articles