开发者

Is there any way to force load a module when it is opened through FSI?

If I compile the following module into a dll

namespace MyNs
module SomeModule =
    do printfn "module loading"
    let x = 23

then reference the dll in FSI and execute the command open MyNs.SomeModule "module loading" does not print immediately. It only prints when I access x which causes all the top level let and do bindings to execute (normal behavior I know in the .NET world). Is there any way, perhaps via an a开发者_StackOverflow中文版ttribute on the module, I can indicate that module should load immediately upon opening in FSI?


Opening a module never does anything at runtime. It just puts all the symbols in the opened namespace in scope for unqualified access below the open statement.

Section 12.5 of the language spec is what you want to read - this details when the static initialization of a module will run.

Given that, the only time when this initialization is run automatically, as far as I know, is for last module in an exe.

I.e. I don't think there is a direct way to accomplish what you want.

If you have reflective access to the module:

ModuleType.TypeInitializer.Invoke(null, null)

will invoke the static initialization.


You can add the AutoOpen attribute to the module

[<AutoOpen>]
module SomeModule =
  do printfn "module loading"
  let x = 23

However this will only print the module loading message when you reference x.


Not sure if you found the solution to your problem but in my case I wanted to start an agent when my website was starting and it was indeed starting twice like you mentioned.

What I did was set a method let start() = inside the module and invoke the method using a static do xxx.start() from my main Site type.

Found that by reading the language spec Kurt linked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜