Skip to content

Instantly share code, notes, and snippets.

@dellisd
Created June 9, 2022 16:37
Show Gist options
  • Save dellisd/a1e2ae1a7e6b61590bef4b2542a555a0 to your computer and use it in GitHub Desktop.
Save dellisd/a1e2ae1a7e6b61590bef4b2542a555a0 to your computer and use it in GitHub Desktop.
Explicit Backing Fields
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.0"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
// Only works with K2 compiler!
kotlinOptions.freeCompilerArgs += listOf("-Xuse-k2")
}
kotlin {
sourceSets.all {
// Must be explcitly enabled!
languageSettings.enableLanguageFeature("ExplicitBackingFields")
}
}
class Test {
val names: List<String>
field: MutableList<String> = mutableListOf<String>()
fun doThing() {
names.add("Hello!")
}
}
fun main() {
val test = Test()
test.doThing()
println(test.names)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment