Eugen Martynov
09/08/2025, 3:58 PMdata class Wrapper(
val data: String = "Test",
val intData: Int = 5
)
fun wrapper(copy: Wrapper.() -> Wrapper) {
Wrapper().copy(....)
}
val wrapper = wrapper { data = "Another" } // Constructs Wrapper("Another", 5)Daniel Pitts
09/08/2025, 4:06 PMcopy(data = "Another")Eugen Martynov
09/08/2025, 4:07 PMDaniel Pitts
09/08/2025, 4:08 PMEugen Martynov
09/08/2025, 4:08 PMEugen Martynov
09/08/2025, 4:09 PMEugen Martynov
09/08/2025, 4:09 PMDaniel Pitts
09/08/2025, 4:10 PMEugen Martynov
09/08/2025, 4:19 PMEugen Martynov
09/08/2025, 4:20 PMcopy function in dsl usage
wrapper {
data = "Another"
}Youssef Shoaib [MOD]
09/08/2025, 5:15 PMDaniel Pitts
09/08/2025, 7:24 PMwrapper(data: String = "Test", intData: Int = 5) = Wrapper(data, intData) If there is some other reason you think a lambda would be useful, maybe that's the part you're caught on?Eugen Martynov
09/08/2025, 7:26 PMDaniel Pitts
09/08/2025, 7:27 PMEugen Martynov
09/08/2025, 7:27 PMEugen Martynov
09/08/2025, 7:27 PMDaniel Pitts
09/08/2025, 7:29 PMEugen Martynov
09/08/2025, 7:29 PMFakeSettings(feature X = true) and I was thinking if I could have dsl to constructor call.Daniel Pitts
09/08/2025, 7:30 PMEugen Martynov
09/08/2025, 7:31 PMYoussef Shoaib [MOD]
09/08/2025, 7:31 PMfakeSettings {
featureX = true
}
Right?
Here's a somewhat cool idea. How about forgetting that data class, and instead using a Set?
Example incoming...Youssef Shoaib [MOD]
09/08/2025, 7:39 PMenum class Setting {
FeatureX
FeatureY
FeatureZ
// As many as you want!
}
typealias Settings = Set<Setting>
class SettingsBuilder(private val underlying: MutableSet<Setting>) {
operator fun Setting.unaryPlus() {
underlying.add(this)
}
operator fun Setting.unaryMinus() {
underlying.remove(this)
}
val Setting.enabled get() = this in underlying
}
val defaultSettings: Settings = setOf() // add your default settings here
inline fun settings(block: SettingsBuilder.() -> Unit): Settings = buildSet {
addAll(defaultSettings)
SettingsBuilder(this).block()
}
// Usage
settings {
+FeatureX
-FeatureY
if(FeatureZ.enabled) -FeatureX
}
Way easier to maintain! FeatureX in settings also works out of the box because Settings is just a Set. You can even not have an enum and instead allow creation of arbitrary features if you really wanted. Bulk operations can be easily added as well.
EnumSet from Java can be used here if you're somehow worried about efficiencyEugen Martynov
09/08/2025, 7:42 PMEugen Martynov
09/08/2025, 7:43 PMEugen Martynov
09/08/2025, 7:43 PMEugen Martynov
09/08/2025, 7:43 PMEugen Martynov
09/08/2025, 7:43 PMYoussef Shoaib [MOD]
09/08/2025, 7:47 PMYoussef Shoaib [MOD]
09/08/2025, 9:05 PM.enabled on get and + and - on set. Then you can easily use the inline refactor in Idea whenever you want to simplify such code.