'do' statements in modules do not get evaluated ?
See original GitHub issueIt seems that do
statements in modules do not get evaluated when the module is accessed from another assembly.
Creating an empty project
D:\>md Test
D:\>cd Test
D:\Test>dotnet new classlib -lang F#
D:\Test>code Library.fs
then changing the code to
namespace Test
module SayDll =
let hello name =
printfn "Hello %s" name
do printfn "loaded-SayDll"
and calling it from fsi
D:\Test>dotnet build
D:\Test>cd bin/debug/net7.0
D:\Test\bin\Debug\net7.0>dotnet fsi
Microsoft (R) F# Interactive version 12.0.2.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> #r "Test.dll"
- Test.SayDll.hello "Goswin";;
--> Referenced 'D:\Test\bin\Debug\net7.0\Test.dll' (file may be locked by F# Interactive process)
Binding session to 'D:\Test\bin\Debug\net7.0\Test.dll'...
Hello Goswin
val it: unit = ()
>
The do
statement was not evaluated.
Whereas putting it all in fsi
D:\>dotnet fsi
Microsoft (R) F# Interactive version 12.0.2.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> module SayFsx =
- open System.Net
- let hello name =
- printfn "Hello %s" name
-
- do printfn "loaded-SayFsx"
-
- SayFsx.hello "Goswin";;
loaded-SayFsx
Hello Goswin
module SayFsx =
val hello: name: string -> unit
val it: unit = ()
the do
statement evaluates as expected.
Is this behavior intentional? Did I miss some documentation on this?
There is also a not really answered question related to this on Stackoverflow.
What would make a do
statement in another assembly’s module evaluate?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top Results From Across the Web
In the `import` syntax of ES6, how is a module evaluated ...
It means that if a module is not included in the main bundle, it will not be evaluated; Modules are singletons. If a...
Read more >Variables I defined in Module are not evaluated as I expected
1 Answer 1 · Thank you. It worked. · But b , f , & c are now global. I would declare them...
Read more >What Happens When a Module Is Imported Twice?
A JavaScript module is evaluated just once. When imported multiple times from the same path, the same module instance is returned.
Read more >Modules, introduction
The browser automatically fetches and evaluates the imported module (and its imports if needed), and then runs the script.
Read more >Check module dependencies and test module code to ...
Solution. To avoid unexpected issues with modules in Puppet Enterprise, make sure to: Test custom modules in PDK. You can use PDK to ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is according to spec.
FSharp language specification 4.1, 12.5.1 execution of static initializers.
The thing causing the difference between FSI and a dll reference is that static initializers are run on the basis of files, and in the first case there is something needing initialization in the same file (because everything is in one file), and in the second it is a “different file”.
If it’s according to spec, I would, at the very least, expect to see a warning when
do
module initializers are used in class libraries.The workaround I have come up with is
It feels very brittle and unintuitive though. Yes, I can use a class with a static ctor for this, but “class bad, module good” 😃))).