Custom getter/setter for properties created by type params


#21

If you want to use a data class you can use this workaround

data class Direction(var vector_: Vector3D) {
    init {
        vector_ = vector_.normalize()
    }

    val vector = vector_
}

not the best but the data class relies on the modified version of your vector. The name in the constructor is slightly different but you can’t access vector in the constructor so no error can be made and you don’t lose the power of the data class (hash and equals) nor the immutability


#22

This example is terrible. Firstly vector is not updated when you change vector_. Secondly you shouldn’t declare properties with backing fields in data classes except in the primary constructor.
If you really need something like this use

data class Foo(private var _test: Vec3) {
    var test: Vec3
        get() = _test
        set(v) {
            _test = transform(v)
        }
    init {
        _test = transform(test)
    }
}

#23

sorry I forgot the private in the constructor in my example.
data class Direction(private var vector_: Vector3D) {
Adding the private in my case shows the case of an immutable data class with custom transformers whereas yours shows a mutable data class with custom transformers.
Thanks for calling my previous example terrible


#24

Yeah, well :blush: always happy to help :wink: Maybe “terribel” is somewhat of an overreaction but reopening a topic after a year to add an example which is wrong is, well :smile: