Type inference for generic parameters when calling a method
I was making a class with builder-type setters for generic member variables and I noticed something when I tried to pass it to a function accepting an object with a specialized generic argument for that type via new. Mainly this:
It appears that the compiler cannot infer the type of
new Foo<>
unless it's specifically told that it's new Foo<Integer>
first off...why? Is the java compiler stupid or something?
second off is there a way around this? Because in my case the generic argument is a little long and looks very ugly.8 Replies
⌛
This post has been reserved for your question.
Hey @nikcho-kouhai! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Okay
Why Can't the Compiler Infer the Type?
that's what I want to know..
There are several approaches to work around this limitation:
Explicit Type Declaration: The simplest way is to explicitly specify the type when creating the instance:
java
bar(new Foo<Integer>().self());
Factory Method: You can create a factory method that returns a Foo<T> with a specific type:
java
These methods allow you to maintain clarity in your code while still leveraging generics effectively without cluttering your main logic with verbose type specifications.
By using these techniques, you can streamline your code and avoid the verbosity of specifying long generic types repeatedly while ensuring that the compiler understands what types you are working with.
This message has been formatted automatically. You can disable this using
/preferences
.Did you fix it now?
well I do know if I tell it what the type is it's gonna work either via <Integer> in the brackets or a method that returns that type. It just kind of bugs me that I have to do that
I suppose there is probably no other way to do it. An odd limitation of the compiler for some reason
thanks for the suggestion anyway
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@359755548973072397>.