Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:33:54+00:00 2026-05-30T10:33:54+00:00

I noticed that, among OCaml programmers I know, some of them always use polymorphic

  • 0

I noticed that, among OCaml programmers I know, some of them always use polymorphic variants (variants that are not declared, prefixed with a backquote), while other ones never use polymorphic variants, and prefer variants declared in types.

Except for performance reasons (polymorphic variants are currently compiled less efficiently than simple variants), how do expert OCaml developers choose between them ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:33:55+00:00Added an answer on May 30, 2026 at 10:33 am

    My usage can be divided into the following 5 categories. 1. interface 2. modularity 3. legibility 4. brevity 5. tricks

    1. If the variant type is only internal to the module, I use regular variants, because as you said they are compiled more efficiently.
    2. If the variant type is exported in the interface and I feel that some cases could appear in other modules but it wouldn’t necessarily make sense to make them dependend on the module, I use polymorphic variants because they are not tied to the module namespace system. Examples: the encoding type type of Xmlm. Also having the signal type as a variant type means you can develop modules using the same idea for XML processing without introducing a dependency on Xmlm.
    3. If the variant type is exported in the interface I find it sometimes too verbose to use regular variants when values of the variant type are given to functions of the module. Example: the version type of Uuidm. Instead of having to write Uuidm.create Uuidm.V4 you can simply write Uuidm.create `V4, which is as clear and less verbose.
    4. Sometimes a particular function may return different cases. If these cases are only used by this function I declare the function type in the interface without having to introduce a type definition. For example parse : string -> [`Error of string | `Ok of t]
    5. Polymorphic variants and their subtyping allow you enforce invariants statically with phantom types. Besides the possibility of defining them incrementally can be useful, both for enforcing invariants statically and for documentation purposes.

    Finally I sometimes use polymorphic variants in the implementation of a module according to 4. but without them showing up in the interface. I discourage this usage unless you declare the polymorphic variants and close them because it weakens the static typing discipline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i noticed that paypal displays a very different favicon, one that's not just a
I've noticed a subtle trend among some developers who consistently place the literal value
I have noticed that MATLAB provides the BLAS and LAPACK headers among others: $
I've noticed that Matlab is very popular among the computer vision and image processing
I've noticed that C-e <END> or M-x move-end-of-line doesn't always move the cursor to
We have some code that removes dangerous attributes and tags from HTML. I noticed
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I noticed that my Designer.vb file of one of my forms has a lot
We noticed that lots of bugs in our software developed in C# (or Java)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.