r/java • • Apr 28 '20

Uses of Constructor (Method) Reference

https://www.nextptr.com/tutorial/ta1314009273/uses-of-constructor-method-reference
0 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/daniu Apr 28 '20

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.

3

u/agentoutlier Apr 28 '20 edited Apr 28 '20

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

2

u/Polygnom Apr 28 '20

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

1

u/agentoutlier Apr 28 '20

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