rspec/rules/S1452/java/rule.adoc

36 lines
911 B
Plaintext

It is highly recommended *not* to use wildcard types as return types. Because the type inference rules are fairly complex it is unlikely the user of that API will know how to use it correctly.
Let's take the example of method returning a "List<? extends Animal>". Is it possible on this list to add a Dog, a Cat, ... we simply don't know. And neither does the compiler, which is why it will not allow such a direct use. The use of wildcard types should be limited to method parameters.
This rule raises an issue when a method returns a wildcard type.
== Noncompliant Code Example
----
List<? extends Animal> getAnimals(){...}
----
== Compliant Solution
----
List<Animal> getAnimals(){...}
----
or
----
List<Dog> getAnimals(){...}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]