cmeeren
cmeeren

Reputation: 4210

Can type constructors be aliased in the same way as types?

Consider the following:

module Module1 =

  type Foo = Bar | Baz

  type Lorem = {Ipsum: int}

module Module2 =

  type Foo = Module1.Foo
  type Lorem = Module1.Lorem

  let foo = Bar // Error: The value or constructor 'Bar' is not defined

  let lorem = {Ipsum = 2} // Error: The record label 'Ipsum' is not defined

Is it possible to alias the types in a simple manner and still be able to actually construct them, without opening Module1?

Background: I am creating an app and a corresponding web API. They will talk to each other by serializing and deserializing POCOs (POFOs?) whose definition is available in a module (corresponding to Module1) in a project referenced by both the app and the web API. The API and the types are versioned, so I'd like to have a Types module (corresponding to Module2) in the app and web API where I simply alias the most recent versions of the API types (e.g. type Foo = Shared.V2.Foo), and then I use these aliases inside the app and API, because that means I don't need references to the specific version all over the app/API. (Please shoot me if this is a terrible idea.)

Upvotes: 1

Views: 71

Answers (1)

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

Your type aliases are completely unused in your existing code. This works if you use them explicitly by qualifying those identifiers like this:

let foo = Foo.Bar

let lorem = {Lorem.Ipsum = 2}

Upvotes: 3

Related Questions