question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make getItems() null-safe for Kotlin interoperability

See original GitHub issue

Hi,

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:closed
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
sockeqwecommented, Mar 16, 2022

it is released now as 4.3.2

Thanks again for your contribution

0reactions
sockeqwecommented, Mar 13, 2022

Sorry, I almost forgot about it. I’m on vacation right now, I’m back on Wednesday. Will then create a new release!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found