like in zig it'd be done with a pointer like so
const Node = struct {
next: ?*Node,
data: i32,
};
in roc syntactically you can just define recursive types
in zig you couldn't write next: Node
because that would be a type of infinite size
but in roc there is implicitly a pointer around any recursive use of a type
so the above would be equivalent to
LinkedList a : [ Nil, Cons a (LinkedList a) ]
ahh thanks. I was looking for a doc on it, thought it wasn't implemented yet.
I do see now it's on here https://github.com/roc-lang/roc/blob/main/roc-for-elm-programmers.md#custom-types
pretty cool tnx
Folkert de Vries said:
so the above would be equivalent to
LinkedList a : [ Nil, Cons a (LinkedList a) ]
one question folkert if you dont mind. why do we need the second a after Cons?
isn't it enough that we specify it after the LinkedList?
[it does seem to work without it but i might be missing something]
here, Cons
has two fields, one of type a
(the data) and one of type LinkedList a
(the next node)
yes I understand. is it just for declarative purposes?
meaning could we have managed without it?
oh nvm
it's more like trying to resemble the structure of the outer type
without that a
there is not actual data in your linked list
it's jus the spine (basically, you've encoded peano numbers at that point)
haha yes
that makes sense. i read it wrong initially
Last updated: Jul 05 2025 at 12:14 UTC