Hello Is it possible to compile and run kotlin scr...
# scripting
z
Hello Is it possible to compile and run kotlin script from other (parent) script, and pass some instances/types created in parent script to child script?
m
I guess you can use
kotlin-compiler-embeddable
and configure the host to pass data to your child script like Gradle does for an exemple to pass a
Project
instance. Never done that myself but that sounds theorically doable
z
Yeah, I use that embeddable compiler. But when I trying to pass objects through
providedProperties
, it throws
Copy code
ERROR wrong number of arguments: java.lang.IllegalArgumentException: wrong number of arguments
Even if child script is empty. There is option
scriptsInstancesSharing
for script configuration, but I don't know how it works.
Sample code that doesn't work: https://github.com/zeganstyl/script-from-script-test And now, script throws other exception:
Copy code
ERROR argument type mismatch: java.lang.IllegalArgumentException: argument type mismatch
e
@Zeganstyl I’m not sure If my this answers your question, but if I understood correctly you can do next: 1. Define whatever classes, interfaces etc in some script, let’s call it model.kts 2. Have two scripts parent.kts and child.kts 3. use @file:Import(“model.kts”) in both parent and child scripts, in addition you should import child script into parent if you want to call some method of child from within parent.
That’s it.
z
@Egor Scripts must be in files? Сan it be implemented through the
SourceCode
interface? Also, I want some isolation between child and parent scripts. So child script will receive only those objects, that parent script passes to child script.
e
I think that it is a bit problematic. There is no support for now doing that. You can evaluate single script using "script contents".toSourceCode(), but there is no way you can use importing other scripts without having them in files. I know this for I needed this as well some time before. The reason is that imporing of scripts us supported for FileBasedSourceCode for now. So yes scripts have to be in files if you want to use imports.
@Zeganstyl all I said here prefixed with AFAIK 😁. It might be that since I last check It was implemented already.