Also, is there a way to detect that a given script...
# scripting
p
Also, is there a way to detect that a given script is the entry-point? In Python you can use:
Copy code
if __file__ == "__main__": hello("sample")
My use case is when using the @\Import feature. Let’s imagine script1.main.kts:
Copy code
@file:Import("script2.main.kts")

hello("john")
and script2.main.kts:
Copy code
fun hello(name: String) = TODO(...)

// MAIN start
hello("sample") // executed even when imported
// MAIN end
👀 5
n
what you could do is have the scripts implement a interfaces or open class and have a
fun main()
in there and call that function after doing the evaluation of the main script the top level statements are always evaluated / executed if you want to avoid that.. put it in a function and then call that function only if you need
1
p
@Nikky thanks for your answer. Sadly it's not solving the issue I described because all functions invocations will be resolved, independently from the fact that they're in the "main" script or not
n
seems like the issue you have is that you want to compile but not invoke dependent scripts and the only way to really do that is to put it into any sort of function.. main was jsut a chosen name here.. you can decide what
interface
/
open class
to use you just need to cast the compiled script object to it at the end
p
@Nikky hum, I guess I need an example to fully understand the idea 😕