nus-cs2030 / 2021-s1

27 stars 48 forks source link

Lab 6 flatMap generics return type #522

Open amandalim857 opened 3 years ago

amandalim857 commented 3 years ago

Hi everything else works already for the whole lab, except for an error that pops up only when a specific T is specified in the Logger return type.

The relevant code:

2020-11-28 2020-11-28 (3)

The output is fine when I remove the Logger<Integer> a = at the start, so i suspect it has to do w my generic ? super/extends return type

2020-11-28 (4)

I'm p sure its something simple but I cant figure it out! Help is appreciated thks

gabrielloye commented 3 years ago

You can try changing the return type of your method from public <U> LoggerImpl<U> flatMap(...) to public <U> Logger<U> flatMap(...)

sharpstorm commented 3 years ago

Maybe you could also try adding an explicit cast when you are creating the new LoggerImpl<U> to change <? extends U> to <U>? Change return new LoggerImpl<U>(mappedVal.get(), newMappedMsg); to return new LoggerImpl<U>((U) mappedVal.get(), newMappedMsg);

itmeruoxin commented 3 years ago

I used LoggerImpl as the return type and it worked, i'm guessing it's the Logger<? extends U> mappedVal, maybe try changing that to LoggerImpl?