Effect CommunityEC
Effect Community17mo ago
1 reply
tobi88

Challenges in Implementing `Schema.suspend` with Generic Types for Recursive Node Structures

Did anyone manage to make Schema.suspend work with a generic type? I have been trying to get this example to work:
https://effect.website/play#64b707598e31

There is a Node that has a children property that are Nodes themselves:
interface Node<Attributes> extends S.Struct.Type<typeof nodeFields> {
  _tag: string
  attributes: Attributes
  children: ReadonlyArray<Node<any>>
}


The challenge arises from defining the Node schema in S.suspend:

const Node = <Attributes extends S.Schema.Any>(
  tag: string,
  attributes: Attributes
) =>
  S.TaggedStruct(tag, {
    ...nodeFields,
    attributes,
    children: S.Array(S.suspend((): S.Schema<Node<any>, NodeEncoded<any>> => Node("?", S.Any)))
  })


I am not so sure anymore if my approach is actually workable. Maybe I am thinking about this the wrong way round.
Was this page helpful?