Open bandytan opened 3 years ago
Hello fellow githubber @bandytan, in this case we need to supress the warning because we are typecasting a more general generic type to a more specific generic type.
However, because we are certain that an empty box can be treated as a box of any generic type, it is safe to suppress this "unchecked" warning.
Hi Bandy,
I think it is because you are confident that the return type is always a Box of type U (a more generic type). Thus, typecasting in this context is okay!
hi there bandy. I believe this is because the method declaration is of Box<\U>. function.apply(object) returns a Box<? extends U> aka a Producer of U so there is a need to typecast it to Box<\U>. java sees this as problematic because of typecasting to a parameterized type Box without 'checking' if it is of a Box<\U> in the first place, hence the name. In this case it is ok to typecast it this way since you are confident that Box<? extends U> behaves like a Box<\U>.
Hi, can I check why we need to typecast even though the function is already declared as Function<? super T, ? extends Box<? extends U>?
@chooweixiang98 Hi Wei Xiang, we have to typecast it into a Box instance as the method that we are defining is an instance of Box. If we do not add in the typecast, the object returned from function.apply(object) may not be an instance of Box as the function can return any instance which extends U. In addition, we know we can safely typecast this ? extends U into Box. Recall how a cheeseburger can be typcasted as a burger in the lectures. Eg. Burger burger = new CheeseBurger(1). Hope this helps!
Anyone knows why in this case it is alright to @SuppressWarnings("unchecked") and type-cast the output to (Box)? I am not too sure when is it acceptable for us to use suppress warnings.