Hello,
I wrote a new lesson on polymorphic variants.
You can find it here.
I appreciate any feedback you may have. I expect that if there is interest in including a lesson on polymorphic variants that there will likely be several rounds of refinement.
Sources:
- Shayne Fletcher: Polymorphic variants : Subtyping and variance
- New Draft Tutorial on Polymorphic Variants
- Is there any kind of guidline about when to use polymorphic variants?
- Polymorphic vs. ordinary variants in ocaml | Yehonathan Sharvit
This lesson is about 800 lines long (about 1100 with line length limited to 85 columns). This makes this lesson on the longer side when compared to other lessons on OCaml.org. Therefore, this is the first of 2 lessons on polymorphic variants.
This lesson (lesson 1) introduces the concepts behind polymorphic variants (such as row polymorphism and structural typing), then discusses common syntactic structures of polymorphic variants. It teaches these concepts in a bottom-up direction. It is my subjective belief (held lightly) that introducing polymorphic variants in a top-down direction leads to more complexity and confusion.
Lesson 2, which is forthcoming, introduces common usecases for polymorphic variants through real-world examples.
Any feedback a reviewer is willing to provide is greatly appreciated. The author is particularly interested in ensuring accuracy and validity of examples and consistency in the language with OCaml.org’s other materials, but all feedback is welcome.