Stream: beginners

Topic: ✔ Tree a: (a, List Tree)


view this post on Zulip Adrian (Mar 28 2024 at 12:25):

What would be a good solution to Tree : (Post, List Tree)
Since the roc compiler states:

── CYCLIC ALIAS in main.roc ────────────────────────────────────────────────────

The Tree alias is self-recursive in an invalid way:

76│  Tree : (Post, List Tree)
     ^^^^

Recursion in aliases is only allowed if recursion happens behind a
tagged union, at least one variant of which is not recursive.

────────────────────────────────────────────────────────────────────────────────

I think, that this type should be allowed, since List may contain 0 items. What would be a good workaround or if this is intended a better solution?

view this post on Zulip Luke Boswell (Mar 28 2024 at 12:49):

Maybe Tree : [Leaf Post, Subtree (List Tree)]? Just on my phone so hard to check

view this post on Zulip Richard Feldman (Mar 28 2024 at 12:52):

@Adrian this should be allowed, but we don't support detecting this case yet

view this post on Zulip Richard Feldman (Mar 28 2024 at 12:54):

as a workaround, you can use an opaque type instead of a type alias

basically change the : to := and then write tree = @Tree (post, list) to create a Tree, and @Tree (post, list) = tree to unwrap a Tree

view this post on Zulip Luke Boswell (Mar 28 2024 at 12:54):

I have a NestSet implementation here which might help with ideas. Theres also the Graph example which might be helpful too.

view this post on Zulip Notification Bot (Mar 28 2024 at 15:32):

Adrian has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC