简体   繁体   English

是否可以在F#中的单个语句中打开给定模块的所有子模块?

[英]Can all submodules of a given module be opened in a single statement in F#?

I have something like this at some part in my app: 我的应用程序中有部分内容是这样的:

open ModuleX
open ModuleX.Submodule1
open ModuleX.Submodule2
open ModuleX.Submodule3
open ModuleX.Submodule4
open ModuleX.Submodule5
open ModuleX.Submodule6

Is there some way of simply doing: 是否有一些简单的方法:

open ModuleX
open ModuleX.*

The worst case in my app is a module that has 27 lines of module opening statements. 在我的应用中,最糟糕的情况是一个模块有27行模块打开语句。 I could break it into different files, but I would only be splitting the openings into different files, not really solving anything. 我可以将其拆分为不同的文件,但是我只会将开口拆分为不同的文件,而不能真正解决任何问题。 Would you advice to "merge" submodules? 您是否建议“合并”子模块? Auto open wouldn't be okay for my case. 自动打开不适用于我的情况。

Or is it just something anyone doing F# has to deal with? 还是只是任何从事F#工作的人都必须处理的事情?

There is no way to control this at the point of opening, but you can make these modules "automatically openable" at their declaration point. 在打开时无法控制它,但是您可以在声明点使这些模块“自动打开”。 To do this, decorate them with [<AutoOpen>] : 为此,请使用[<AutoOpen>]装饰它们:

module A =

  [<AutoOpen>]
  module B =
    let x = 5

  module C =
    let y = 6

open A

let z = x   // x is now available
let u = C.y // y still requires qualified access

People who bought [<AutoOpen>] also liked [<RequireQualifiedAccess>] :-) 购买[<AutoOpen>]也喜欢[<RequireQualifiedAccess>] :-)

[<RequireQualifiedAccess>]
module C =
  let y = 6

open C // Compiler error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM