#Why is this return value not of the generic type
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
its probalby something obvious
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
ah this is a fun one
so the problem is that the caller specifies T
not your code
so like I could make a type
enum Fuck implements IVariant { INSTANCE; }
and call that method
... why fuck
Fuck f = GenderVariant.MALE.getDefault();
wtf is this hidden message
how would this even be possible
thats an illegal cast
oh because theyre both T i guess
try returning null from getDefault
ye that works i know
so then how do i do this
since the caller picks the T you can't return any T
so this whole structure isnt possible
bro both of u moved threads get back here and help me!
record Box<T>(T value) {}
it works here tho
try making it return Box<? extends Enum<?> & IVariant> getDefault()
Box?
so the caller can't pick it
whats Box?
yeah
oh
thats weird
yeah
what does that do
whats the purpose of it, looks like it wouldnt rly do anythign
nope
well it lets you return ? extends A & B without making a T that a called can specify
don't have it be T
don't use T
just have Box<? extends`
ohhhh
forgot about wildcards
what....
im insane or somehting
why does that not work
i guess & doesn't work with wildcards
shit
yeah, & is really restricted
this works
ok but it doesnt work for where i use it
static <T extends Enum<?> & IVariant> T getWithName(Class<T> instance, String name){
return Arrays.stream(instance.getEnumConstants()).filter(t -> t.getName().equals(name)).findFirst().orElse(instance.getEnumConstants()[0].getDefault().value);
}```
Detected code, here are some useful tools:
static < T extends Enum<? > & IVariant > T getWithName(Class<T> instance, String name) {
return Arrays.stream(instance.getEnumConstants()).filter(t -> t.getName().equals(name)).findFirst().orElse(instance.getEnumConstants() [0] .getDefault().value);
}
bruh it formatted it worse
yeah i dont get why the bot does that
is it really that hard to make it not do that prompt when it detects code in ` blocks?
in 90% of cases the prompt is helpful. in the other, click the delete button and it goes away
if u don't want it, don't click on the format button
that said, the prompt only triggers for OP
not for helpers
ohh, i didnt realize that was a personal message only
whenever i had these prompts pop up in my threads i just ignored them because i wasnt entirely sure what the buttons would do
oh actually ig its not personal? idk
anyway they annoy me but im sure theyre a net good towards providing useful threads ๐
well tehy would be good but they dont rly format it in a good way imo
this is split mid-method
in most cases they do
they are meant for beginners who post unformatted unreadable code. which is like 80% of people posting here
for everyone else, just click the trashbin icon and it goes away and stops annoying u
oh
and because of this specific use case, it also has to yield okayish results for buggy code with syntax mistakes and non-java code.
which is why its overall quality isn't the best for rather niche Java features or edge cases
a proper java formatter fails utterly if there's a single syntax error or when it's non-java
fair
why T exdends Enum<?> and not T exdends Enum<T>
enums are self-types
if you want this to be a polymorphic method, turn your interface into a self-type too
implements IVariant<GenderVariant>
and interface itself is interface IVariant<T extends Enum<T> & IVariant<T>>
this way, you can remove all the generic declarations from your method
although, what's the point of that since it would be an instance method anyway
i ended up changing it it now works, and is waaaaay simpler