开发者

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

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

import开发者_Python百科 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜