Hey guys, In order to be able to use a lens do I n...
# arrow
k
Hey guys, In order to be able to use a lens do I need to know the specific subtype? I have a sealed class with its subtypes, which contains an abstract list of objects:
Copy code
@optics
sealed class AbstractUser : User() {
    abstract val infos: List<UserInfo>

   //subclassing data classes here
}
At some point I need to modify the
infos
list and I don’t know which subtype it is. I don’t suppose it’s possible to use a lens here, is it?
p
Why does it need to be abstract? can’t it be initialized to empty list or be a constructor parameter?
k
Because I want all subclasses to implement it. If it's a constructor parameter then subclasses will only shadow it which is not ideal, but could probably live with it. Would that change anything though?
p
All subclasses will need to pass the parameter to the cnstructor, doesn’t that achieve the same? What does shadow mean here?
k
Shadow as in there will actually be two instances created: one inside
AbstractUser
and one inside
FunnyUser
(a subclass). As I say: not a huge deal, but not ideal for me either
p
It won’t, right? If you have the field on the parent it propagates to the children too, even if they override them
Copy code
sealed class Bla(val a: String)

class Ble(): Bla(a = "1")
object Miau: Bla(a = "33")
Copy code
Ble().a // "1"
Miau.a // "33"
k
I’m not sure what you actually mean. Here is what I’d do in my case:
Copy code
sealed class Bla(open val a: String) {
    class Ble(override val a: String) : Bla(a)
    object Miau: Bla(a = "33")
}

Bla.Miau.a
Bla.Ble("2").a
There will be only one
a
for
Bla
and
Ble
accessible, but if you open it in debugger, each object has its own copy. Does this matter for the optics at all though?
p
but if you open it in debugger, each object has its own copy
¿? it’s the same reference, it shouldn’t be a copy
even if it is, it’s some microperf that shouldn’t matter for immutability