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 8908431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:10:36+00:00 2026-06-15T03:10:36+00:00

I have two modules. One defines a variant type: module A = struct type

  • 0

I have two modules. One defines a variant type:

module A = struct
  type foo = Bar of material | Baz | Boo

  (* other stuff *)
end

and I would like to be able to use foo‘s variants both as constructors and as left-hand-sides in another module

module B = struct
  type foo = A.foo  (* I can abbreviate A.foo by assigning it a local alias *)

  let f (x : foo) = match x with
    | Bar m       -> Bar (g m)  (* Any way to abbreviate Bar and friends? *)
    | Baz   | Boo -> x
end

but per “referring to named objects” I have to prefix the variant names with a module-path:

  let f (x : foo) = match x with
    | A.Bar m         -> A.Bar (g m)
    | A.Baz   | A.Boo -> x

Is there any way to skip avoid using the module path short of opening and pulling in all the other stuff from A?

  • 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-06-15T03:10:37+00:00Added an answer on June 15, 2026 at 3:10 am

    You can open A locally:

    let f (x : foo) = A.(match x with
      | Bar m       -> Bar (g m)
      | Baz   | Boo -> x)
    

    or

    let f (x : foo) =
      let open A in
      match x with
      | Bar m       -> Bar (g m)
      | Baz   | Boo -> x)
    

    You can define Bar in a submodule so that less things are exposed:

    module A = struct
      module BasicDataAndOps = struct
        type foo = Bar of material | Baz | Boo
      end
      open BasicDataAndOps
      (* other stuff *)
    end
    
    module B = struct
      open A.BasicDataAndOps
      ...
    

    For use outside of patterns, you can define a “smart constructor” in B:

    let bar m = A.Bar m
    

    ETA: I forgot about the possibility to restate type definition, described in Ashish Argwal’s answer: type foo = A.foo = Bar of material | Baz | Boo. Given that you already have type abbreviation in your example, this is the best answer.

    There is some work on type-based label disambiguation which could be helpful, but it may not get accepted into the language.

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

Sidebar

Related Questions

I have two different modules that need access to a single file (One will
I have written a module similar to the following: module One class Two def
I have two backbone Views. One is the main view and the other is
Let's say I have two modules. One is core and another is core dependent
I have two resolvers configured. One is the company repo, the other a local
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port
I am working on a transliteration tool. I have two modules lexer and translator.
I have these two modules : package G1; sub new { my $class =
In my code base I find that two modules have structures with the same
I have an application (CC) containing two modules: CC-ejb and CC-war. CC-ejb contains JPA

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.