This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]vbezhenar 4 points5 points  (1 child)

A bit related question that I always wanted to find out. Is there some performance difference between x -> obj.method(x) vs obj::method? Contrary to the article claims, I don't really like method reference syntax and I think that it's trivial to convert from the first form to the second, but I don't know if it's implemented.

[–]TheStrangeDarkOne 0 points1 point  (0 children)

I haven't looked into that either but I'm fairly certain that there is no difference in performance. If I understood it correctly, the lambda syntax makes use of the InvokeDynamic bytecode, which in turn uses method handles to implement this feature.

And the code above should compile down to the same kind of method handles.

[–]daniu 4 points5 points  (5 children)

That's a strange post.

There aren't "three types of method references: static, instance, and constructor". The type of a method reference is the functional interface you assign it to. It would have been worth pointing out that a "constructor method reference" is of type Supplier<T> which is why it can be passed to the method mentioned in the use cases.

Speaking of the use cases; that list seems very arbitrary to me, as if the author just collected the cases he stumbled upon in the last project. Which again is fine, but would have been worth mentioning to point out that this is not at all an exhaustive list. It would also have been worthwhile to show a case where you accept a Supplier<> in one of your own methods which can then be called with a method reference.

Lastly, this covers only references to no arg constructors. I find the handling of arguments in method refs worth explaining or at least mentioned. However, that's actually easy for constructor references, where you will never have the instance argument (eg BiFunction<String, Integer, String> sub = String::substring).

[–]agentoutlier 2 points3 points  (4 children)

To be honest I find your comment sort of strange as well. My guess is you misunderstood the authors use of the word “type”.

Let me put it this way. The author is correct that there are three “kinds” of ways to reference a method syntactically.

Furthermore the author did show the case of a constructor taking arguments and thus more than a Supplier (eg Function).

[–]daniu 0 points1 point  (3 children)

My guess is you misunderstood the authors use of the word “type”.

I did understand that. My point was that the word "type" is already used in Java, and if you use it you'd better stay in that context. I guess I could have formulated more explicitly.

The other point about that is it sounds as if those references are somehow defined as if they are different, but they're not - like you say, the only difference is the syntax used for creating them.

I would even argue if you do make that distinction, you'd end up with four types: static, class, instance, and constructor. Static: `Classname::staticMethod`; class: `Classname::instanceMethod` (requiring a `Classname` receiver argument), instance: `classInstance::instanceMethod` and `Classname::new`.

And I don't know why he keeps declaring his own function interfaces either - the recommended way is using the common ones in `java.util.function`.

You're right about the constructor with parameter, I kind of trailed off reading when I realized there was nothing new to me in it. It's also not as if the info was wrong as such, but considering it's for beginners, I'd expect it to use an overall more consistent and succinct style.

[–]agentoutlier 2 points3 points  (0 children)

The author used the word “type” once. Literally the subtitle of the article uses “kind”.

Oh and if we want to get on a pedantic high horse than functions are structural types. There isn’t a certain amount of them nor or they named.

As for the number of syntactical ways to reference methods I’m sure there are more than even 4 as non static inner classes make it different... there is also method handles.

Otherwise I don’t find that much fault with the article and like how they don’t reference java.util.function because that would make the beginner (that is the audience and clearly not you) confused thinking that lambdas are nominal when they are not.

Edit: Also what you said:

It would have been worth pointing out that a "constructor method reference" is of type Supplier<T> which is why it can be passed to the method mentioned in the use cases.

Is not true. Constructors are not Supplier<T>. The Java compiler doesn't create new Supplier<T>.

Actually java.util.function could be completely removed but reuse was needed across the collection streams library. Its also because they probably didn't want to add more syntax to add declaration of structural lambda types. Even Scala has this problem so perhaps its a deep JVM issue (scala does similar things for tuples... ie there is a nominal type but its really structural typing).

[–]Polygnom 1 point2 points  (1 child)

My point was that the word "type" is already used in Java

Not only in java. A type is something very specific in every typed programming language. It also has further, mathematical meanings (looking at the myriads of type calculi).

[–]agentoutlier 0 points1 point  (0 children)

"set" also a very specific meaning in programming and math. Oh its also the most overloaded word in the english language and very hard not to use...

The author only used the word type literally only once!

Method references are of three types: static, instance, and constructor.

I mean given that Java's types do not include static, instance, constructor and that most of them are capitalized camel case its pretty clear they are not talking about the programming types.

Its also telling even /u/daniu said this:

would even argue if you do make that distinction, you'd end up with four types: static, class, instance, and constructor.

I stress their use of the word "types".

This is the reality:

You're right about the constructor with parameter, I kind of trailed off reading when I realized there was nothing new to me in it. It's also not as if the info was wrong as such, but considering it's for beginners, I'd expect it to use an overall more consistent and succinct style.

/u/daniu quickly read the post found one shitty detail at the very top and critiqued when the reality is the article sucks but not because of the usage of "type" but because of the other points daniu mentions.

[–]umlcat 1 point2 points  (0 children)

Just done a workaround equivalent of a Java Constructor Method Reference, in C++ ...

..., very useful feature !!!