Hello, is there a way in Kotlin to use property de...
# multiplatform
k
Hello, is there a way in Kotlin to use property delegates for data classes in constructor? I want to create something like Swift's
Property Wrapper
https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID617
k
b
data class Entity: InterfaceOfSmth by ClassThatImplementsSomethingOfThatInterface()
k
@Kris Wong no. I didn't find how change getter for data class property 😢
b
You can always add secondary constructor that delegates props from args
k
Swift's
Property Wrapper
changes getter's logic inside binary representation (how i understood)
k
kotlin has the same concept. add a normal property to your data class and provide the getter and setter.
k
@Big Chungus but than i will lost all data class features (like auto
toString()
method)
b
Nope
You'll just do the conversion of your constructor args at instantiation. Resulting class will still be the same
k
No. For example: If i use property wrapper than system log prints property via custom getter. But data class doesn't use property delegate for generating toString() method.
b
Then as @Kris Wong mentiones override getters
This still won't break equals and toString
Remember that every val and var have getters setters behind them
k
No,
toString()
method uses properties without getters but i want to change value before print it to log

https://www.youtube.com/watch?v=nIGlxcUES_Qâ–¾

How I understand that Kotlin hasn't feature like
Property Wrapper