Vampire
05/31/2023, 3:32 PMdata class Foo(val foo: Nothing? = null) {
companion object {
fun foo() {
println(__FILE__)
}
}
}
fail compilation with
error: object Companion captures the script class instance. Try to use class or anonymous object instead
and is there a way to mitigate it?Ilya Muradyan
05/31/2023, 3:54 PMclass C(val __FILE__: File) {
inner data class Foo(val foo: Nothing? = null) {
companion object {
fun foo() {
println(__FILE__)
}
}
}
}
And of course you can't access class' properties from its companion (or its inner class companion).
There is basically no way to mitigate this in K1 (unless you don't refer script properties from companion)Vampire
05/31/2023, 3:57 PM__FILE__
in the default value of a parameter of Foo
and in the companion object I call that constructor, but that also counts as referring. ๐Vampire
05/31/2023, 3:57 PM__FILE__
would be someway different and could be used anywhere in the file.Ilya Muradyan
05/31/2023, 4:00 PMVampire
05/31/2023, 4:01 PMIlya Muradyan
05/31/2023, 4:08 PMclass Foo {
companion object {
fun foo(obj: Any) {
val f = // obtain __FILE__ property of obj with reflection
println(f)
}
}
}
Foo.foo(this)
Vampire
05/31/2023, 4:17 PMVampire
05/31/2023, 4:18 PM__FILE__
should be special and available in the whole file.Vampire
05/31/2023, 4:19 PMVampire
05/31/2023, 4:31 PMVampire
05/31/2023, 4:31 PMVampire
05/31/2023, 4:31 PMdata class Foo(val foo: Nothing? = null) {
companion object
}
fun Foo.Companion.foo() {
println(__FILE__)
}
ilya.chernikov
06/06/2023, 9:32 AM__FILE__
is just that) is limited by the language rules, in particular singletons cannot capture anything.
The extension functions are not limited, of course.
This limitation is arising on the translation to the JVM, and so far I don't see how we can overcome it in the K2 too, so probably it will remain a limitation at least in the initial K2 support.
Theoretically, we can try to make __FILE__
a special value that is not limited by these rules, but I'm not yet sure it is important enough to spend an effort on.
Anyway, it would be helpful if you'll file an issue about __FILE__
in YT.Vampire
06/06/2023, 10:57 AM