Skip to content

Instantly share code, notes, and snippets.

@dellisd
Created June 9, 2022 16:37
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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