# Representing recursive structs

**URL:** <https://discourse.dry-rb.org/t/representing-recursive-structs/508>\
**Category:** Support\
**Created:** [March 11, 2018, 8:02pm UTC](https://discourse.dry-rb.org/t/representing-recursive-structs/508 "2018-03-11T20:02:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dvdsgl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dvdsgl/32/235_2.png) [@dvdsgl](https://discourse.dry-rb.org/u/dvdsgl)\
**Post date:** [March 11, 2018, 8:02pm UTC](https://discourse.dry-rb.org/t/representing-recursive-structs/508/1 "2018-03-11T20:02:19Z")

</div>

I’m trying to represent mutually recursive structs in Ruby to model this data:

```json
{
  "data": 123,
  "next": {
    "data": 234,
    "next": {
      "data": 3456,
      "next": null
    }
  }
}

```

```ruby
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`?

---

<div class="post-metadata">

**Author:** ![dvdsgl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dvdsgl/32/235_2.png) [@dvdsgl](https://discourse.dry-rb.org/u/dvdsgl)\
**Post date:** [March 11, 2018, 9:03pm UTC](https://discourse.dry-rb.org/t/representing-recursive-structs/508/2 "2018-03-11T21:03:51Z")

</div>

Nevermind! I’ve forward-declared the structs with:

```ruby
# (forward declaration)
class List < Dry::Struct; end

# (forward declaration)
class Next < Dry::Struct; end

```

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [March 12, 2018, 9:14pm UTC](https://discourse.dry-rb.org/t/representing-recursive-structs/508/3 "2018-03-12T21:14:01Z")

</div>

I think this should be good enough for you but in future there can be complications with recursive data types. For instance, we’ll need to figure out how to dump them to AST properly.
