Dear All, does somebody know whether is is possibl...
# scripting
j
Dear All, does somebody know whether is is possible to open the REPL with a pre-loaded script template ? It would be great to open the REPL and then be able to execute (interactive) script commands as if I were writing a script within my extension. At the minimum, I would need to open the REPL with some pre-loaded imports so that script objects and functions are recognized.
a
i would also find that useful. but then a question is... how does the dependency management work there? with java.lang /stdlib it's a non-issue but what if you want to import custom apis, deal with versioning and transitive dependency etc
j
My idea was to gather all dependencies (perhaps as an über-jar) as pass them beforehand as -cp parameters. Of course this means that all transitive dependencies must be known ahead of time.
Building on this last idea, if kotlinc had an "interactive" option (say -i) which could be combined with -script to that it executed the script and then stayed in the resulting REPL, we could do something like "kotlinc -cp {dependency jars} -i -script MyInitScript.kts", where the init script contains the import directives, declarations, etc. to configure the environment. That would be somewhat like having a poor man's scripting template.
a
that would be really wonderful. because a big limitation of REPLs for me is takes so much work to build up to the context needed.
i
Standard Kotlin REPLs (the
kotlinc
one and the IntelliJ counterpart) do not support custom script definitions yet. But you can build your own relatively easily. The easiest way will be to use JSR-223 with
main-kts
, see the example here - https://github.com/JetBrains/kotlin/blob/1.3.70/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsJsr223Test.kt#L39 More advanced things are possible with the new REPL API which is introduced with 1.4 (starting from -M3, which is not published yet). Here you can have a look at the https://github.com/Kotlin/kotlin-jupyter for inspiration. And if you'll run a REPL e.g. with
main-kts
definition, the preconfiguration mode you described is easily achievable with
@Import
annotation - you can configure things in a script and then simply
@Import
it into the REPL.
😍 1