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.

Usability issue with CliktCommand.findOrSetObject() being lazy

See original GitHub issue

When in the main command’s constructor doing something like

init {
    findOrSetObject {
        loadConfig() // Return a populated OrtConfiguration object.
    }
}

without any further access of the object, and in the sub-command initializing a member property like

private val config by requireObject<OrtConfiguration>()

this results in a KotlinNullPointerException. The reason is that CliktCommand.findOrSetObject() does not actually store the object in the context until it’s read for the first time.

While I was able to work around this by calling the underlying Context.findOrSetObject() directly in the main command’s run() like

override fun run() {
    context.findOrSetObject { loadConfig() }
}

maybe there is a way to avoid API misuse like this altogether?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ajaltcommented, Jan 27, 2020

Yes. If you want to set the object eagerly, you should call the function on the context.

0reactions
ajaltcommented, May 22, 2020

I updated the documentation to mention that the property delegate is lazy, so hopefully this will reduce confusion in the future.

@ppslim Thanks for the input! I’m not a fan of to declaring an unused property just to cause some side effects. I think it’s more clear to call the function on your context directly, so I’d rather not add an eager parameter to findOrSetObject .

Read more comments on GitHub >

github_iconTop Results From Across the Web

clikt - bytemeta
clikt repo issues. ... Usability issue with CliktCommand.findOrSetObject() being lazy. sschuberth. sschuberth CLOSED · Updated 2 years ago ...
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