#Why is this return value not of the generic type

1 messages ยท Page 1 of 1 (latest)

astral axle
#

I don't see how it doesnt meet the requirements T does.

civic flickerBOT
#

<@&987246399047479336> please have a look, thanks.

astral axle
#

its probalby something obvious

civic flickerBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

ancient orchid
#

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

astral axle
#

... why fuck

ancient orchid
#
Fuck f = GenderVariant.MALE.getDefault();
astral axle
#

wtf is this hidden message

astral axle
#

thats an illegal cast

#

oh because theyre both T i guess

ancient orchid
#

try returning null from getDefault

astral axle
#

ye that works i know

ancient orchid
#

an then the Fuck code will work

#

thats the problem

astral axle
#

so then how do i do this

ancient orchid
#

since the caller picks the T you can't return any T

astral axle
ancient orchid
#

hmm

#

good question

#

idk

#

just guessing

wheat pier
#

bro both of u moved threads get back here and help me!

ancient orchid
#
record Box<T>(T value) {}
astral axle
#

it works here tho

astral axle
#

bro

#

wtf

wheat pier
#

im kidding ๐Ÿ˜‚

#

but please

ancient orchid
#

try making it return Box<? extends Enum<?> & IVariant> getDefault()

astral axle
#

Box?

ancient orchid
#

so the caller can't pick it

astral axle
#

whats Box?

ancient orchid
#

yeah

astral axle
#

oh

astral axle
ancient orchid
#

yeah

astral axle
#

what does that do

#

whats the purpose of it, looks like it wouldnt rly do anythign

ancient orchid
#

don't have it be T

#

don't use T

#

just have Box<? extends`

astral axle
#

ohhhh

#

forgot about wildcards

#

what....

#

im insane or somehting

#

why does that not work

ancient orchid
#

i guess & doesn't work with wildcards

astral axle
#

shit

ancient orchid
#

yeah, & is really restricted

astral axle
#

this works

ancient orchid
#

hmm

#

intrestin

astral axle
#

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);
    }```
civic flickerBOT
astral axle
#

bruh it formatted it worse

solar light
#

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?

cloud bough
#

if u don't want it, don't click on the format button

#

that said, the prompt only triggers for OP

#

not for helpers

solar light
#

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 ๐Ÿ™‚

astral axle
#

well tehy would be good but they dont rly format it in a good way imo

#

this is split mid-method

cloud bough
#

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

astral axle
#

oh

cloud bough
#

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

astral axle
#

fair

minor dew
#

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

astral axle