Hi. I’m making new class for variable type which h...
# announcements
s
Hi. I’m making new class for variable type which has string value like below.
Copy code
class CustomFieldTypeA() {
    var value: String = ""
    private val charset: Charset = ACharset()
}
class CustomFieldTypeB() {
    var value: String = ""
    private val charset: Charset = BCharset()
}

data class TestData(
    var field1: CustomFieldTypeA = null,
    var field2: CustomFieldTypeB = null,
)
The reason to use CustomFieldType is to have charset for string. And I want to use this type in the same way to use String like below
Copy code
// for setter
val obj = TestData()
obj.field1 = "hi type 1"
obj.field2 = "hi type 2"
// for getter
val str = obj.field1
print(str) // hi type 1
Is there any way to do like this? (maybe using delegate?)
t
I still haven't quite understood the purpose of your extra classes, but you basically already gave the answer yourself. Just implement the
getValue
and
setValue
operator functions in those classes and use them as delegates (
var field1: String by CustomFieldTypeA()
) Get more info on how to implement the delegate here: https://kotlinlang.org/docs/delegated-properties.html
s
Thanks for your answer 🙂 But I’m wondering if getValue/setValue of delegate could be defined as different type with variable type. For example, getValue return String but the field type is not String(CustomFieldTypeA). Is it possible to define it??
Sorry for being asking this basic one. I’m quite new to here 🙏
t
What's the point in having a "different type" if you want both the getter and the setter to work with Strings?
s
As shown in the first code block, I want a type with string value and specified charset. And don’t want to make the way to use the get / set not comfortable.
t
Yes, but isn't that just relating to the internal handling you implement in your delegate classes? If you want it to behave as a String every time you get or set it, the property type should ideally be a String. If that's not what you want, it might be better (or at least easier to understand) to just add
.value
to your calls when getting and setting the value. BTW something else to think about: do you really need 2 different implementations or would it be enough to just have one implementation and provides the desired charset in the constructor?
of course there are some more things you could do: 1: this allows you to access the delegate instance that is used when getting or setting field1
Copy code
var delegate1 = CustomFieldDelegateTypeA()
var field1: String by delegate1
2: this would be a simpler approach
Copy code
var field1 = CustomFieldTypeA()
var value1: String
    get() = field1.value
    set(value) { field1.value = value }
s
oh.. I see. it’s nice way to use delegate. But in my case there’s lots of fields to behave like it. Now I got that using delegate for my purpose is not that ideally good. The field seems better to be String. And Charset is kindof metadata so it’s proper to add it as annotation.
Thanks for your time and kind help!
e
there is a shortcut to delegate directly to a KProperty:
Copy code
val container = Container(value = "")
var property by container::value