Skip to content
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

Init inside Object in Kotlin Script #482

Closed
fiddlededee opened this issue Jan 30, 2024 · 1 comment
Closed

Init inside Object in Kotlin Script #482

fiddlededee opened this issue Jan 30, 2024 · 1 comment

Comments

@fiddlededee
Copy link

I use clikt in kts files and it is perfect. Could you please clarify one issue.

This code works as intended:

class Options1 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
    init { main(args) }
}
println(Options1().input)

So does this one (the one I use):

object Options2 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
}
Options2.main(args)
println(Options2.input)

But this one fails (would be cleanest in simple cases, IMHO):

object Options3 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
    init { this.main(args) }
}
println(Options3.input)

The message is error: object Options3 captures the script class instance

@ajalt
Copy link
Owner

ajalt commented Jan 31, 2024

That error isn't from Clikt, it's from Kotlin itself, so there's nothing I can do about it on my end.

The first result from googling that error message was this Kotlin issue: https://youtrack.jetbrains.com/issue/KT-32410

Seems like it will be fixed in Kotlin 2.0.

@ajalt ajalt closed this as completed Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants