GTDev
GTDev

Reputation: 5528

How to define mutually recursive types

I have the following code:

type cellInfo('a) = {
  index: int,
  column: column('a),
  id: string
};

type cellInfoFn('a) = cellInfo('a) => ReasonReact.reactElement;

type column('a) = {
  header: string,
  accessor: accessor('a),
  id: option(string),
  cell: cellInfoFn('a),
};

see how each type relies on each other.

I am getting the following error message:

[1]   12 │ type cellInfo('a) = {
[1]   13 │   index: int,
[1]   14 │   column: column('a),
[1]   15 │   id: string
[1]   16 │ };
[1]
[1]   This type constructor's parameter, `column`, can't be found. Is it a typo?

How do i handle mutually recursive type definitions?

Upvotes: 2

Views: 403

Answers (1)

glennsl
glennsl

Reputation: 29106

Mutually recursive definitions must be separated by and. This goes for both let definitions and type definitions:

type cellInfo('a) = {
  index: int,
  column: column('a),
  id: string
}

and cellInfoFn('a) = cellInfo('a) => ReasonReact.reactElement

and column('a) = {
  header: string,
  accessor: accessor('a),
  id: option(string),
  cell: cellInfoFn('a),
};

Upvotes: 5

Related Questions