Created
June 9, 2022 16:37
-
-
Save dellisd/a1e2ae1a7e6b61590bef4b2542a555a0 to your computer and use it in GitHub Desktop.
Explicit Backing Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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