# Module initialize

**URL:** <https://forums.fsharp.org/t/module-initialize/3613>\
**Category:** General\
**Tags:** beginners\
**Created:** [April 27, 2024, 2:47pm UTC](https://forums.fsharp.org/t/module-initialize/3613 "2024-04-27T14:47:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![altbodhi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/altbodhi/32/1253_2.png) [@altbodhi](https://forums.fsharp.org/u/altbodhi)\
**Post date:** [April 27, 2024, 2:47pm UTC](https://forums.fsharp.org/t/module-initialize/3613/1 "2024-04-27T14:47:57Z")

</div>

```auto
module Test
printfn "hello"

```

Why if it module load in fsx  
in console will be printed message, but in fspro if open module it does not?  
In most languages with modules if we uses module it always init. for example [Визуальные интерактивные модели для образования и науки / Ви́жуал СФУ / Оберон Онлайн (oberon.org)](https://online.oberon.org/)

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [April 27, 2024, 8:07pm UTC](https://forums.fsharp.org/t/module-initialize/3613/2 "2024-04-27T20:07:08Z")

</div>

From what I can tell, it has to actually _use_ something from the module for it to do the init. It’s not enough to just open it.  
If you do

```auto
module Test
printfn "hello"
let finishTxt = "do" + "ne"

```

and then in Program.fs do

```auto
open Test
printfn "%s" finishTxt

```

then you will see “hello” printed out. Interestingly, if you define  
`let finishTxt = "done"`  
then you _won’t_ see “hello” printed out, presumably because it just inlined finishTxt as a literal value without actually evaluating anything in the module.

You can count on any initialization at the top of a module being run before any of the contents of the module, but I would recommend against trying to otherwise control exactly when any initialization runs (e.g., by the order of `open` statements). I think the F# compiler sort of decides when exactly the initialization of a module gets run.

---

<div class="post-metadata">

**Author:** ![realparadyne](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/realparadyne/32/169_2.png) [@realparadyne](https://forums.fsharp.org/u/realparadyne)\
**Post date:** [May 3, 2024, 3:48pm UTC](https://forums.fsharp.org/t/module-initialize/3613/3 "2024-05-03T15:48:13Z")

</div>

AFAIK, an F# module maps to a .NET Class. The Class has a static constructor, and those are only run the first time the class is instantiated, or a static method is called (which is what finishTxt becomes, a static method of a Class).

So, this is a .NET thing and not an F# thing. C# and VB and C++/CLI would get the same behavior for any .NET classes.

---

<div class="post-metadata">

**Author:** ![Martin521](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/martin521/32/1179_2.png) [@Martin521](https://forums.fsharp.org/u/Martin521)\
**Post date:** [May 3, 2024, 7:48pm UTC](https://forums.fsharp.org/t/module-initialize/3613/4 "2024-05-03T19:48:00Z")

</div>

If you want to learn about the details of F# static initialization, check §12.5.1 of the [F# 4.1 spec](https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf). (Details may have changed in later language versions).
