https://kotlinlang.org logo
#scripting
Title
# scripting
g

Geert

03/22/2024, 3:13 PM
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

Adam S

03/22/2024, 3:15 PM
Yes, it is. If you're looking for an example, I like this guide: https://scastiel.dev/kotlin-scripts-dsl-underrated-feature
👀 1
g

Geert

03/22/2024, 3:16 PM
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

Adam S

03/22/2024, 4:19 PM
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

Geert

03/22/2024, 5:34 PM
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

Adam S

03/22/2024, 9:39 PM
nice!