sdgfsdh
sdgfsdh

Reputation: 37131

Can I import just one declaration from an F# module?

Suppose I have a module like this:

module Foo

let x = 1
let y = 2

Now I can use this module like this:

module Bar

let z = Foo.x + Foo.y

Is it possible to import a definition from Foo such that it does not need to be qualified?

Something like:

module Bar

import x from Foo // Not real code

let z = x + Foo.y // x need not be qualified

Note that I do not want to import everything from Foo

Upvotes: 4

Views: 340

Answers (1)

Aaron M. Eshbach
Aaron M. Eshbach

Reputation: 6510

You cannot, there is no direct F# equivalent to the ES6 import { ... } from 'Module' syntax. F# supports organizing code into both modules and namespaces, but both modules and namespaces are 'imported' in their entirety with the open keyword. As mentioned in the comments, you can use local bindings to simplify qualified access to values (such as let exchangeRange = Conversions.Currency.UsdToCadExchangeRate) or type aliases to simplify qualified access to types (type Converter = Conversions.Currency.CurrencyConverter).

In addition, modules can be marked with the [<AutoOpen>] attribute to make their contents accessible without qualified access, or the [<RequireQualifiedAccess>] attribute to make their contents accessible only when qualified, even if the module is referenced in an open expression.

See this MSDN article for more information.

Upvotes: 5

Related Questions