Usability issue with CliktCommand.findOrSetObject() being lazy
See original GitHub issueWhen 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:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top 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 >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
Yes. If you want to set the object eagerly, you should call the function on the context.
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 tofindOrSetObject
.