Thread
#compose
    s

    Se7eN

    1 year ago
    Probably not compose related, but how can I create an immutable copy of an object from a mutable one?
    class Element {
        var position: Offset by mutableStateOf(...)
        var rotation: Float by mutableStateOf(...)
        ...
    }
    
    // Then I can do this:
    val element = Element(...)
    element.position = newPosition
    I want to pass this
    element
    object to a composable but don't want it to be mutable. I can create a similar class for an immutable element but then I'd have to update the immutable class whenever I change the
    Element
    class. Is there something else I can do?
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    Do you need immutability or just "read-only”? That is, do you need to ensure that the value you pass into the composable never ever changes, or do you just need to ensure that the composable doesn’t change the object itself? If the latter, then you could make a read-only version of the Element interface that the mutable Element extends, and just specify that your composable takes the read-only interface.
    s

    Se7eN

    1 year ago
    I need to ensure the composable doesn't change the properties of
    Element
    . Don't really understand what's the difference between immutability and read-only here 🤔 But I'll try the interface thing and see if that's what I want
    Thanks, it works!
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    Read-only means that a thing doesn’t provide a way to mutate it, but it might still be mutated by someone else (e.g.
    List
    vs
    MutableList
    – a
    MutableList
    is a
    List
    , but not the other way around, so if you only have a
    List
    you can’t change it but the list can still be changed out from under you if it’s a
    MutableList
    under the hood and someone else is mutating it). Immutable is a stronger guarantee, meaning a thing can’t change at all.