# \#polymorphic-variants

**URL:** https://discuss.ocaml.org/tag/polymorphic-variants/460.md

[Latest](https://discuss.ocaml.org/latest.md) · [Categories](https://discuss.ocaml.org/categories.md) · [Tags](https://discuss.ocaml.org/tags.md)

---

## [Polymorphic variant type gets restricted when try with is used](https://discuss.ocaml.org/t/polymorphic-variant-type-gets-restricted-when-try-with-is-used/16016)

<div class="topic-metadata">

**Author:** [@aiono](https://discuss.ocaml.org/u/aiono)\
**Replies:** 2\
**Last updated:** [January 24, 2025, 9:43am UTC](https://discuss.ocaml.org/t/polymorphic-variant-type-gets-restricted-when-try-with-is-used/16016 "2025-01-24T09:43:58Z")

</div>

In the following code: type t = \[\`A | \`B\] let foo : (unit, \[\> t\]) result = failwith "todo" exception Ex of t let bar : (unit, \[\> t\]) result = try Ok (failwith "todo") with Ex t -\> Error t type of bar is inferred…

---

## [Type checking pattern matching on polymorphic variants](https://discuss.ocaml.org/t/type-checking-pattern-matching-on-polymorphic-variants/15922)

<div class="topic-metadata">

**Author:** [@osa1](https://discuss.ocaml.org/u/osa1)\
**Replies:** 3\
**Last updated:** [January 10, 2025, 6:34pm UTC](https://discuss.ocaml.org/t/type-checking-pattern-matching-on-polymorphic-variants/15922 "2025-01-10T18:34:00Z")

</div>

let x () = \`A let \_ = match x () with | \`A -\> () let \_ = match x () with | \`A -\> () | \`B -\> () In this program I’m wondering when does the type checker “close” the variant in the return type of x (), t…

---

## [Modeling Rust trait objects subtyping relation via OCaml polymorphic variants](https://discuss.ocaml.org/t/modeling-rust-trait-objects-subtyping-relation-via-ocaml-polymorphic-variants/15644)

<div class="topic-metadata">

**Author:** [@Konstantin\_Olkhovski](https://discuss.ocaml.org/u/Konstantin_Olkhovski)\
**Replies:** 2\
**Last updated:** [November 21, 2024, 9:42am UTC](https://discuss.ocaml.org/t/modeling-rust-trait-objects-subtyping-relation-via-ocaml-polymorphic-variants/15644 "2024-11-21T09:42:32Z")

</div>

I’m trying to model Rust subtyping around trait objects in OCaml. I have made a universal smart pointer for ocaml-rs (repo here if someone is interested), which allows to store Rust objects or Rust trait objects within s…

---

## [Figuring out how to return an open polymorphic variant from application to a module](https://discuss.ocaml.org/t/figuring-out-how-to-return-an-open-polymorphic-variant-from-application-to-a-module/15292)

<div class="topic-metadata">

**Author:** [@s.t.s](https://discuss.ocaml.org/u/s.t.s)\
**Replies:** 2\
**Last updated:** [September 18, 2024, 12:45am UTC](https://discuss.ocaml.org/t/figuring-out-how-to-return-an-open-polymorphic-variant-from-application-to-a-module/15292 "2024-09-18T00:45:03Z")

</div>

I stumbled upon an interesting problem when attempting to figure out a way to nicely and elegantly encode tags for data in a modular fashion. First, we may want to approach the problem in an elementary fashion: module …

---

## [A pattern for using GADTs to subset cases](https://discuss.ocaml.org/t/a-pattern-for-using-gadts-to-subset-cases/13675)

<div class="topic-metadata">

**Author:** [@polytypic](https://discuss.ocaml.org/u/polytypic)\
**Replies:** 19\
**Last updated:** [September 17, 2024, 12:24am UTC](https://discuss.ocaml.org/t/a-pattern-for-using-gadts-to-subset-cases/13675 "2024-09-17T00:24:59Z")

</div>

Hello! The topic of using GADTs comes up every now and then. Personally I find that when I’m using GADTs in an attempt to optimize code I almost always quickly run into some “surprise” that doesn’t pass the type checke…

---

## [New Draft Tutorial on Polymorphic Variants](https://discuss.ocaml.org/t/new-draft-tutorial-on-polymorphic-variants/13485)

<div class="topic-metadata">

**Author:** [@cuihtlauac](https://discuss.ocaml.org/u/cuihtlauac)\
**Replies:** 24\
**Last updated:** [November 30, 2023, 2:00pm UTC](https://discuss.ocaml.org/t/new-draft-tutorial-on-polymorphic-variants/13485 "2023-11-30T14:00:51Z")

</div>

Dear OCamlers, The OCaml.org team continues working on new tutorials. We have a draft on polymorphic variants; we’d like your feedback on it: GH PR: https://github.com/ocaml/ocaml.org/pull/1531 Online draft: Polymorph…

---

## [Understanding polymorphic variants](https://discuss.ocaml.org/t/understanding-polymorphic-variants/13500)

<div class="topic-metadata">

**Author:** [@mobileink](https://discuss.ocaml.org/u/mobileink)\
**Replies:** 2\
**Last updated:** [November 26, 2023, 9:04am UTC](https://discuss.ocaml.org/t/understanding-polymorphic-variants/13500 "2023-11-26T09:04:04Z")

</div>

\[Starting a new thread on grounds that New Draft Tutorial on Polymorphic Variants is for feedback on the tutorial\] Exploring polymorphic variants I tried this: # let f = function | \`Cat -\> "pet" | \`Dog -\> "pet" | 99 -\>…

---

## [Modelling access permissions with polymorphic variants](https://discuss.ocaml.org/t/modelling-access-permissions-with-polymorphic-variants/12251)

<div class="topic-metadata">

**Author:** [@Konstantin\_Olkhovski](https://discuss.ocaml.org/u/Konstantin_Olkhovski)\
**Replies:** 6\
**Last updated:** [June 15, 2023, 10:38am UTC](https://discuss.ocaml.org/t/modelling-access-permissions-with-polymorphic-variants/12251 "2023-06-15T10:38:18Z")

</div>

I’m trying to model a set of permissions as polymorphic variants, used later as phantom type in signatures to control required permissions to call certain functions. In the snippet below Permissions module represents pre…

---

## [Mapping GADT with Polymorphic Variant into a more restrictive form](https://discuss.ocaml.org/t/mapping-gadt-with-polymorphic-variant-into-a-more-restrictive-form/12214)

<div class="topic-metadata">

**Author:** [@Ben\_James](https://discuss.ocaml.org/u/Ben_James)\
**Replies:** 5\
**Last updated:** [May 24, 2023, 9:13pm UTC](https://discuss.ocaml.org/t/mapping-gadt-with-polymorphic-variant-into-a-more-restrictive-form/12214 "2023-05-24T21:13:44Z")

</div>

Hello! Suppose I have the following GADT in Ocaml: type \_ t = | A : f t | B : f t \* f t -\> f t | C : c t \* c t -\> f t | D : \[\< f | c\] t \* c t -\> c t | E : c t | T : string -\> \[\< f | c\] t where f and c are …

---

## [Signature of a function changed when adding a recursion](https://discuss.ocaml.org/t/signature-of-a-function-changed-when-adding-a-recursion/11905)

<div class="topic-metadata">

**Author:** [@kttmm](https://discuss.ocaml.org/u/kttmm)\
**Replies:** 2\
**Last updated:** [April 7, 2023, 7:35pm UTC](https://discuss.ocaml.org/t/signature-of-a-function-changed-when-adding-a-recursion/11905 "2023-04-07T19:35:31Z")

</div>

I have a function whose type is changed when introducing a recursive calling function with and. The change in type breaks callers somewhere else in the code (spooky action at distance!). The function before the change: …

---

## [Deconstructing polymorphic variant type](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984)

<div class="topic-metadata">

**Author:** [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Replies:** 5\
**Last updated:** [December 17, 2022, 7:22pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984 "2022-12-17T19:22:44Z")

</div>

Is it possible to deconstruct a polymorphic variant type such that this does what you would intuit it should? let foo = function | \`OK a -\> Ok a | otherwise -\> Error otherwise And the pseudo code for the type would…

---

## [Inferred types and polymorphic variants](https://discuss.ocaml.org/t/inferred-types-and-polymorphic-variants/8427)

<div class="topic-metadata">

**Author:** [@viritrilbia](https://discuss.ocaml.org/u/viritrilbia)\
**Replies:** 8\
**Last updated:** [September 7, 2021, 9:00am UTC](https://discuss.ocaml.org/t/inferred-types-and-polymorphic-variants/8427 "2021-09-07T09:00:57Z")

</div>

I understand why (1) when the output of a function is a polymorphic variant type, it gets a lower bound from the tags used in the function, and (2) when the input of a function is a polymorphic variant type, it gets an u…

---

## [Empty polymorphic variant type use case](https://discuss.ocaml.org/t/empty-polymorphic-variant-type-use-case/8371)

<div class="topic-metadata">

**Author:** [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Replies:** 2\
**Last updated:** [August 27, 2021, 1:17pm UTC](https://discuss.ocaml.org/t/empty-polymorphic-variant-type-use-case/8371 "2021-08-27T13:17:43Z")

</div>

Hello. I was wondering with some of my colleagues what the use case could be for an empty polymorphic variant type, apart from making some type that is empty at first but meant to be extended afterwards in other modules. …
