I'd like to have a script def that treats the scri...
# scripting
r
I'd like to have a script def that treats the script as if it is an object/class body instead of a method body. Is this possible at the moment? Could I achieve it via a compiler plugin (probably not much point in going there until K2)? Is there a currently tracked issue for it?
i
Currently the script is treated under the hood as a class body. The statements are virtually packed into
init
blocks, variables become properties of this class and functions - methods of this class. The evaluation of the script is in fact an instantiation of the script class, and the scripting API allows you to get an instance of this class after eval. There is no way currently to "compile script into method body" and there is an issue about it. But there is no guarantee that we will ever provide such an option per se. In general we would like to treat script as something special with it's own properties and guaranties, without exposing implementation details. So, if you have some particular reasons why you want certain representation of the script to be accessible, please tell us. We will try to consider them while designing scripting support in K2. But we most likely will try to hide the actual implementation details.
r
Ah TIL, I thought that scripts were treated as a method body. If it's already done as a class I should be able to do what I want. My use case is essentially using scripts to provide pluggable components, which would be objects.
Follow up to this since I've been able to investigate more: is there any way to force a script to override abstract methods? I.e. have the host class be abstract. For more context, I essentially want to use scripts as "definitions" of types/objects, rather than actual scripts. Loading (compiling + executing) one of the scripts would result in an object that I could pass around and call the user-defined methods on. Think game moding or even Gradle build scripts (just with tasks modelled as methods). I'd also probably want a compiler plugin to extract some annotated methods to a super-interface, but as long as scripting works with compiler plugins I don't expect that to be an issue.
i
There is no such a way now, unfortunately. And we are not sure it is a good model to support generally. But we may consider exposing a way to do something in that direction via a plugin in K2.
r
👍 A plugin would be fine for my use case