New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document Kotlin declaration site variance subtleties #31370
Comments
I suspect this kind of behavior is caused by Kotlin declaration site variance. When you declare class ListToImmutableListConverter : Converter<List<Any>, ImmutableList<out Any>> {
override fun convert(source: List<Any>): ImmutableList<out Any> = ImmutableList.copyOf(source)
} I know this is pretty subtle but this is how Kotlin deals with lists and when mixing with Java list, there is a need to be aware of the declaration type variance to get type comparison working as expected. I am not sure there is something to fix on Spring Framework side for this specific use case, but please let me know what you think about it. |
Thanks very much, @sdeleuze. That fixes the problem. If there are other areas in Framework where type comparison may not work when using |
Sure, that's a good idea, and that will give me an opportunity to document some guidance for injection related to #22313. As a consequence, I turn this issue into a documentation one. |
An update from the question on Stack Overflow: using |
Affects: 5.3.x
For background, this problem was identified while looking at https://stackoverflow.com/questions/77233335/how-to-register-new-collection-factories-in-spring-boot.
With a
Converter
implemented in Kotlin for aList
to Guava'sImmutableList
conversion:ResolvableType
resolves the firstAny
to?
and the second toObject
:Contrastingly, using Java and
?
results in both being resolved to?
:Resolving the target type to
ImmutableList<Object>
rather thanImmutableList<?>
is problematic as it prevents the converter from matching aList<String>
toImmutableList<String>
conversion.The text was updated successfully, but these errors were encountered: