Marcono1234 commented on pull request #451:
URL: https://github.com/apache/logging-log4j2/pull/451#issuecomment-751490879


   > and you can say that any method is 'not type-safe' if it is declared in 
the patten `<T> T foo()`.
   
   And I will say this 😄
   Though to clarify, the issue I am having with this is that the return type 
is not bound, i.e. none of the parameters of method (in this case there are no 
parameters) refer to this type parameter. Using type parameters which are bound 
as return type is perfectly fine.
   
   Personally I think such constructs should be used very sparingly and only 
where it is absolutely necessary. There is a reason why the compiler complains 
about the implementation of this method in `AbstractLogger` and you have to use 
`@SuppressWarnings("unchecked")` to suppress that warning.
   
   The problem with these unbound return types is that the user of the API 
might not even notice it because to them it just looks like any other method. 
But is allows them to write something like the following which won't cause any 
errors during compile time, not even any warnings, but will break during 
runtime:
   ```java
   interface MyCustomMessageFactory extends MessageFactory {
   }
   
   Logger logger = LogManager.getLogger();
   // Compiles without any warnings or errors
   MyCustomMessageFactory f = logger.getMessageFactory();
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to