Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I selectively open a module so I can refer to some of its values without qualification?

Tags:

module

f#

In Haskell, I use the Data.Map module, and its principal type of the same name, Data.Map.Map, like this:

import Data.Map (Map)
import qualified Data.Map as M

In F#, I want to do something similar with my Item module, which contains a type of the same name:

module Item

type Item = { Description: string }
let empty = { Description = "" }

I can't find a way to use this module qualified and the type unqualified. Can I use this module and type like this, from another module?

let getItem (): Item = Item.empty

Edit:

Adding a type alias from a client module lets me use the Item module with qualification and the Item type without qualification, but is there a better way still?

type Item = Item.Item
like image 387
David Siegel Avatar asked Jan 21 '23 20:01

David Siegel


1 Answers

I think the only way to import only a single type from a module is to use type alias (as you already noted). To get a qualified access to members of the module (under a different name), you can use module alias:

type Item = Item.Item // Type-alias for type(s) from module
module I = Item       // Module-alias for accessing members

// Now you can write:
let getItem() : Item = I.empty

I don't think there is any way for importing members from module selectively (as in Haskell), so this is probably the best option.

like image 114
Tomas Petricek Avatar answered Jan 30 '23 09:01

Tomas Petricek