I was looking in the kotlin-script-examples projec...
# scripting
g
I was looking in the kotlin-script-examples project and wondering if it is possible to use your own class as a base class for the script to run in (so that the
this
of the file is of a type of your own choosing), a la gradle?
👌 1
a
Yes, it is. If you're looking for an example, I like this guide: https://scastiel.dev/kotlin-scripts-dsl-underrated-feature
👀 1
g
Awesome! Will have a look!
Hmm, that doesn’t really change the
this
of the file, if I understand correctly.
However, it gave me an idea: what I really wanted was to use Kotlin script build some configuration. That I can do with some dsl that just returns the configuration I’m interested in.
But it requires the user to have the dsl invocation as the last statement. 🤔
a
hmmm I thought it did allow setting the base class, see step 3 here https://kotlinlang.org/docs/custom-script-deps-tutorial.html#create-a-script-definition
In scriptDef.kt, create a class. It will be a superclass for scripts of this type, so declare it abstract or open.
g
I’ll try to get it running, then I can see and I’ll let you know. First invalidate some caches. 😉
You were right, I got the example running and added this in the script:
Copy code
val x = this
println(x.javaClass.superclass)
result:
Copy code
class nwt.script.NwtScript
a
nice!