uli
11/26/2024, 10:28 AMprivate val greeting = "Hello World"
private fun greetingFactory() = greeting
internal inline fun greet() {
println(greetingFactory())
}
fun main() {
greet()
}
https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMi4wLjIxIiwiYXJncyI6IiIsIm5vbmVNYXJrZXJzIj[…]luKCkge1xuICAgIGdyZWV0KClcbn0iLCJwbGF0Zm9ybSI6ImphdmEifQ==Vampire
11/26/2024, 10:33 AMuli
11/26/2024, 10:35 AMuli
11/26/2024, 10:35 AMgreet
is inlined into another file, it would produce code accessing the file private method greetingFactory
from another file.Vampire
11/26/2024, 10:37 AMVampire
11/26/2024, 10:39 AMinternal
the inline function is only usable within the same compilation unit, so such problems cannot arise and it is probably acceptable to inline that call unless it fails then at runtime due to private. 🤷♂️Vampire
11/26/2024, 10:41 AMuli
11/26/2024, 10:41 AMmain
in a different file, it will probably throw at runtime. At least that’s what happens in my project.
• This code compiles, even if I access greeting
directly. In my original project the compiler complains when I access greeting
, but not when I access greetingFactory
. I’ll try to reproduce that.uli
11/26/2024, 10:42 AMVampire
11/26/2024, 10:44 AMVampire
11/26/2024, 10:45 AMVampire
11/26/2024, 10:46 AMuli
11/26/2024, 10:57 AMprivate static final String greeting = "Hello World";
private static final String greetingFactory() {
return greeting;
}
public static final void greet() {
int $i$f$greet = false;
String var1 = access$greetingFactory();
System.out.println(var1);
}
// $FF: synthetic method
public static final String access$greetingFactory() {
return greetingFactory();
}
uli
11/26/2024, 10:58 AMuli
11/26/2024, 11:12 AMNon-private inline function cannot access members of private classes: 'public data object O1 defined in com.test.test.Objects'
for below code. But it compiles and runs even when main is in a different file.
private sealed interface Objects {
data object O1
}
private fun objectFactory() = Objects.O1
internal inline fun greet() {
println(Objects.O1)
}
Vampire
11/26/2024, 12:13 PMuli
11/26/2024, 12:16 PMObjects.default
with Objects.O1
it compiles and runs.
If I replace Objects.default
with indirect access through private fun defaultObject
, it compiles and runs.
private sealed interface Objects {
companion object {
val default = O1
}
data object O1 : Objects
}
private fun defaultObject(): Objects = Objects.default
internal inline fun getDefaultRoute(): Any {
return Objects.default
}
uli
11/26/2024, 12:19 PMObjects.O1
and did not trust AS that it would not accept Objects.default
Vampire
11/26/2024, 12:28 PM