azabost
10/31/2024, 2:47 PMinterface Foo {
val xyz: String
}
class Bar : Foo {
override val xyz: String
init {
xyz = compXyz("XYZ")
}
fun compXyz(key: String) = key.reversed()
}
fun main() {
val s = Bar()
println(s.xyz)
}
I'm asking because I seem to get this warning in Android Studio (I use Kotlin 1.9.24 in the project):
Property must be initialized, be final, or be abstract. This warning will become an error in future releases.
and not sure how to interpret this issue: https://youtrack.jetbrains.com/issue/KT-57553/Implement-deprecation-for-open-val-with-backing-field-and-deferred-initialization-in-K1
i.e. whether this is going to be deprecated soon or not
I don't see any issues running it in Kotlin Playground though (tried both 1.9 and 2.0) 🤔Joffrey
10/31/2024, 2:53 PMclass Bar : Foo {
override val xyz: String = compXyz("XYZ")
fun compXyz(key: String) = key.reversed()
}
azabost
10/31/2024, 2:53 PMJoffrey
10/31/2024, 2:54 PMJoffrey
10/31/2024, 2:56 PMBar
was open
, but it's not in your case. So if you still have the warning with this, it must be a false positive. Did you reproduce the warning in Android Studio with the example you gave here?Joffrey
10/31/2024, 2:59 PMBar
were open
, then you would actually be exposed to the issue mentioned in the ticket, and you could prevent it by making xyz
final
in Bar
(to prevent custom setters).azabost
10/31/2024, 3:00 PMinterface BuildVariables {
val foo: String
}
abstract class BuildVariablesService : BuildService<BuildVariablesService.Params>, BuildVariables {
internal interface Params : BuildServiceParameters {
val propertiesFile: Property<File>
}
private val properties: Properties = Properties().apply {
val file = parameters.propertiesFile.get()
file.bufferedReader().use { load(it) } // reading a file here
}
override val foo: String = getEnvOrProperty("FOO")
private fun getEnvOrProperty(key: String, default: String? = null): String =
getEnv(key) ?: properties.getProperty(key, default)
}
I was considering this:
abstract class BuildVariablesService : BuildService<BuildVariablesService.Params>, BuildVariables {
internal interface Params : BuildServiceParameters {
val propertiesFile: Property<File>
}
private val properties: Properties = Properties()
override val foo: String
init {
val file = parameters.propertiesFile.get()
file.bufferedReader().use { properties.load(it) } // reading file here
foo = getEnvOrProperty("FOO")
}
private fun getEnvOrProperty(key: String, default: String? = null): String =
getEnv(key) ?: properties.getProperty(key, default)
}
azabost
10/31/2024, 3:02 PMJoffrey
10/31/2024, 3:04 PM2.0
in the playground: https://pl.kotl.in/AEYyF3DJMazabost
10/31/2024, 3:04 PMJoffrey
10/31/2024, 3:04 PMazabost
10/31/2024, 3:05 PMazabost
10/31/2024, 3:06 PMabstract
in my actual use caseJoffrey
10/31/2024, 3:06 PMazabost
10/31/2024, 3:06 PMCLOVIS
11/04/2024, 8:43 AMprivate val properties: Properties = Properties()
init {
val file = parameters.propertiesFile.get()
file.bufferedReader().use { properties.load(it) } // reading file here
}
override val foo: String = getEnvOrProperty("FOO")
you're guaranteed that the init block will run before foo
's initialization, so you should be good here