Piotr Krzemiński
02/21/2023, 1:17 PMgildor
02/21/2023, 1:23 PMPiotr Krzemiński
02/21/2023, 1:24 PMgildor
02/21/2023, 1:25 PMCLOVIS
02/21/2023, 1:25 PMPiotr Krzemiński
02/21/2023, 1:26 PMPiotr Krzemiński
02/21/2023, 1:27 PMgildor
02/21/2023, 1:27 PMphldavies
02/21/2023, 1:34 PMCopyWithoutNamedArguments
compilation warning on .copy(...)
usage without named arguments - it would be nice to have a @RequireNamedArguments
annotation you can add where required to provide a similar warning (which could be done using #detekt rule for now)Adam S
02/21/2023, 2:17 PMPiotr Krzemiński
02/21/2023, 2:18 PMthe only strict way I can think of is to make every argument a value class, which is verbose, annoying to use, and complicated to set up.I’d prefer the good old Java builder over this 🙂 but I probably won’t implement it
But to share this with everyone requires an IntelliJ plugin...yeah, exactly - the same with linter rules
Adam S
02/21/2023, 2:20 PM.main.kts
, then an IJ plugin is more tempting...Piotr Krzemiński
02/21/2023, 2:26 PMAdam S
02/21/2023, 2:43 PMimport kotlin.properties.Delegates.notNull
class Parameters {
var foo: String by notNull()
var bar: Int by notNull()
override fun toString() = "Parameters(foo=$foo,bar=$bar)"
}
fun someFunction(init: Parameters.() -> Unit) {
val params = Parameters()
params.init()
println(params)
}
fun main() {
someFunction {
foo = "abc"
bar = 123
}
}
Playground linkPiotr Krzemiński
02/21/2023, 2:44 PMBut it’s possible to forget to add parameters.exactly - we’d sacrifice one kind of safety measure for the other, I’d rather stay with the current approach, If we had a mechanism to enforce using all intended parameters in a lambda, then it’s a sensible thing to consider
phldavies
02/21/2023, 3:02 PMsealed interface Marker
fun forcedNamedArguments(vararg _ignore: Marker,
name: String,
age: Int,
nickname: String = name) {
println("$name ($nickname) is $age years old")
}
fun main() {
forcedNamedArguments(
name = "Robert",
age = 32,
nickname = "Bob"
)
}
Piotr Krzemiński
02/21/2023, 3:05 PMphldavies
02/21/2023, 3:30 PMsealed interface
approach as there’s no need for the suppressions, and the only downside is someone could call it with myFun(*emptyArrayOf<Marker>(), "Robert", 32)
Piotr Krzemiński
02/22/2023, 7:32 AMthe only downside is someone could call it with (…)good point, however it would have to be a conscious decision, not an accident