Win a copy of Kotlin Cookbook this week in the Kotlin forum!

Germans Zaharovs

Greenhorn
+ Follow
since Jan 11, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
3
Given in last 30 days
3
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Germans Zaharovs

Now I understand, it's basically impossible)) I was confused of implementation of additional argument & thought it might possible. Surely it makes no sense to change functional interface to accommodate method reference:)

Many thanks!:)
1 week ago
Hello Stephen, thanks for your help.

I have tried multiple times using different approaches of what you've written, but still can't get some of statements. If I correctly understand, that instance methods are eventually built using additional param of instance, but this shouldn't change functional interface.

By examples:



The above code fails. Wondering, where should I write, if not in functional interface?:


2) strReverse is an instance method of MyStringOps, so we prepend a parameter: (MyStringOps, String) -> String.



If functional interface rewritten code compiles:


I guess, maybe my English isn't good & I can't pick something... could you show by above examples, if it is possible to use instance method without using object & changed functional interface?
1 week ago
Stephen,

So as far as I understood and played around, non-static referenced class methods would expect to receive first param in functional interface object itself e.g. :



Above code compiles and runs as expected now:)

So would it be correct to assume following:


Foo::doSomething;
is equivalent to

OR





I have tried to search for any other references apart of one mentioned by Junilu:


i'm not convinced that's what's happening though. I've been going through the JLS section on Method Reference Expressions https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.13 to see which rules apply here but haven't been able to find anything yet. This is a good question.



Would appreciate, if someone would share this.

Thanks a lot Stephen & Junilu! You helped to understand topic far better.
2 weeks ago
Hello,

I have just started to go through 'Java. The Complete Reference' book 9th edition (JDK 8).
On chapter 15 p. 397 started learning 'Method References to Instance Methods' & played around. All examples in book make sense & works, but, I have trouble to understand why would following code works:




Whilst following method isn't:



I know, that failing code is using class to get instance method, not instance itself. However, as per book, this code works (using generics, but still I think similar functionality):




Compiler of failing java code gives result:

$ javac -Xdiags:verbose -d bin src/book/lambda/MethodReferenceInstance.java
src\book\lambda\MethodReferenceInstance.java:27: error: method stringOps in class MethodReferenceInstance cannot be applied to given types;
       System.out.println(stringOps(MyStringOps::strReverse, "Lambda"));
                          ^
 required: StringFunc,String
 found: MyStringOp[...]verse,String
 reason: argument mismatch; invalid method reference
     cannot find symbol
       symbol:   method strReverse(String)
       location: class MyStringOps
1 error

I was trying to find out regarding this compiler error, but it's not that easy... Wondering, would someone in simple terms could explain why above code is failing?

2 weeks ago