Is there a way to do a ```if (isFoo) { return ...
# scripting
v
Is there a way to do a
Copy code
if (isFoo) {
    return
}
in a Kotlin Script, specifically a Gradle Kotlin DSL script in this case? Or do I really have to put all the code below into the parens and invert the condition?
I think it might be magically resolved by https://youtrack.jetbrains.com/issue/KT-28140/Generate-script-into-a-method but don't know when that'll happen
v
D'oh! Thanks.
a
v
That would be horrible. I want to skip the rest of that script, not end the entire process. ;-)
m
fun end() { throw EndOfScriptException() }
v
I don't find that exception
And if you meant to throw a custom exception, how would that be different from
exitProcess
, except that the exit is not with success. It would not help at all.
m
exitProcess
exits the process, it doesn't throw an exception (it looks like it does, but that's just there to satisfy the compiler, it won't be called). The idea is use a custom exception (or actually you'd inherit from
Error
to stop it being caught accidentally) and then catch it in your scripting host. Then you can do any tidy ups before quitting normally.
It's the only way, as script code is compiled into constructors and the only way to abort a constructor is by throwing
v
Yeah, as I said, this does not help in any way for the question
My script host is Gradle. I have several precompiled script plugins. And I want to only early-out successfulyl in that one plugin while not disturbing the remaining processing.
m
Right, sorry, I thought it's a general scripting question but you said specifically for Gradle. You would have to patch Gradle in that case. AFAIK there's no other way.
e
it's possible (and easy) to modify Gradle such that you can
throw GradleScriptReturn()
to simulate an early return from within a buildscript, really just one line of non-boilerplate code. whether you should is a different question of course :p
🧠 1
👍 1