I’m trying to represent mutually recursive structs in Ruby to model this data:
{
"data": 123,
"next": {
"data": 234,
"next": {
"data": 3456,
"next": null
}
}
}
class List < Dry::Struct
attribute :data, Types::Strict::Int
attribute :next, Types.Instance(Next).optional
end
class Next < Dry::Struct
attribute :data, Types::Strict::Int
attribute :next, Types.Instance(List)
end
But Ruby errors because Next
is referenced before it’s defined. How do you recommend breaking cycles like this when using dry-types
?