Is it ok to write interfaces that require field access on subtypes?

aleferna12
Sun, 07 May 2023 04:04:06 +0000
New to Julia

Hello!

Since we can’t define fields on abstract types (at least yet, see this very long discussion), I was wondering whether the following design pattern is considered good practice in Julia:

abstract type A end

struct B <: A
    x::Int
end

getx(a::A) = a.x

I’m asking because if the following struct is later defined:

struct C <: A end

then the call to getx(C()) will not work, thus breaking the Liskov substitution principle.

Is this not a concern?

The alternative of course would be to write individual getters/setters for each struct, which quickly becomes a lot of boiler-plate code.

How one should go about this?

Cheers

7 posts - 5 participants