What does square brackets `[` and `]` mean in a data type definition in OCaml?
Asked Answered
C

1

0

I saw the following:

type rlevel = [
| `rlevel
]

but I've never seen this before and the tutorials for ADTs (algebraic data types) are not super helpful nor is the OCaml grammar.

What does this mean?

Classroom answered 14/7, 2022 at 18:51 Comment(0)
P
1

The type rlevel is a type abbreviation for a polymorphic variant type (https://ocaml.org/manual/polyvariant.html) which contains only one constructor `rlevel .

Purcell answered 14/7, 2022 at 18:59 Comment(2)
ok so I can name a constructor with 'rlevel? What would be an example value?Classroom
Rather `rlevel is polymorphic variant constructor and polymorphic variants are structural. Thus `rlevel is a value and the only inhabitant of type [ `rlevel ]Purcell

© 2022 - 2024 — McMap. All rights reserved.