is string interpolation performed at runtime or co...
# announcements
d
is string interpolation performed at runtime or compile-time?
d
Runtime mostly.
f
well kinda both, it changes the interpolation to StringBuilder.append calls at compile time but the result is done at runtime obviously
d
that's basically what I was asking, thanks
r
@fred.deschenes That's how all string concatenation is compiled as far as I am aware.
f
yup
d
just wanted to make sure it wasn't parsing at runtime
f
nah it takes the actual value at runtime
d
Even if the values are known at compile time?
f
pretty sure it does (because even a const val can be messed with at runtime with reflection AFAIK), let me check
d
I thought const vals where inlined so it wouldn't matter.
f
yeah you're right, val is still done at runtime, const vals are inlined
Copy code
val foo = "foo"
const val bar = "bar"

fun main(args: Array<String>) {
    println("This is foo : $foo")
    println("This is bar : $bar")
}
gives me this bytecode:
Copy code
Compiled from "Main.kt"
public final class MainKt {
  public static final java.lang.String bar;

  public static final java.lang.String getFoo();
    Code:
       0: getstatic     #12                 // Field foo:Ljava/lang/String;
       3: areturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #18                 // String args
       3: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #26                 // class java/lang/StringBuilder
       9: dup
      10: invokespecial #30                 // Method java/lang/StringBuilder."<init>":()V
      13: ldc           #32                 // String This is foo :
      15: invokevirtual #36                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      18: getstatic     #12                 // Field foo:Ljava/lang/String;
      21: invokevirtual #36                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      27: astore_1
      28: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
      31: aload_1
      32: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      35: ldc           #53                 // String This is bar : bar
      37: astore_1
      38: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
      41: aload_1
      42: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      45: return

  static {};
    Code:
       0: ldc           #7                  // String foo
       2: putstatic     #12                 // Field foo:Ljava/lang/String;
       5: return
}
r
@Dominaezzz "inlined" can be vague. It could just mean the values are inlined into the concatenation, but it seems the Kotlin compiler is smart enough to make a plain string.
d
Ah, good point! I meant copy and pasted.