Make getItems() null-safe for Kotlin interoperability
See original GitHub issueHi,
we are using a ListDelegationAdapter
in our Kotlin project and everything is working nicely.
There is only one thing I would love to have and that is a null-safe access to the items
variable declared in the AbsDelegationAdapter
.
Currently Kotlin does not enforce nullability when calling items
(or getItems()
).
We can build something like this as a superclass for our adapters:
abstract class BridgedListDelegationAdapter<T : List<Any>> : ListDelegationAdapter<T> {
constructor() : super()
constructor(delegatesManager: AdapterDelegatesManager<T>) : super(delegatesManager)
constructor(vararg delegates: AdapterDelegate<T>) : super(*delegates)
// Java to Kotlin bridge for null-safety
@Suppress("RedundantOverride")
override fun getItems(): T? {
return super.getItems()
}
}
Apart from being an ugly workaround, it also will only work for accessing items
outside of the adapter. In a sub-class, it will still default to the protected T items
variable without null-safety.
Is there anything that can be done about that? Or am I missing an obvious solution to that problem? Thank you!
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Null safety - Kotlin
Null safety. Nullable types and non-null types. Kotlin's type system is aimed at eliminating the danger of null references, also known as ...
Read more >Null safety in legacy Java libraries used in Kotlin projects
Any reference in Java may be null, which makes Kotlin's requirements of strict null-safety impractical for objects coming from Java. (.
Read more >Retrofitting null-safety onto Java at Meta
We developed a new static analysis tool called Nullsafe that is used at Meta to detect NullPointerException (NPE) errors in Java code.
Read more >Null-safe calls marked as partially uncovered #182 - GitHub
I suspect that the uncovered branch is the case that value is not null, but its toString method returns null. However, the compiler...
Read more >Kotlin Nullable Non Nullable Safety - Javatpoint
Use of external java code as Kotlin is Java interoperability. ... To make string which holds null value, we have to explicitly define...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
it is released now as
4.3.2
Thanks again for your contribution
Sorry, I almost forgot about it. I’m on vacation right now, I’m back on Wednesday. Will then create a new release!