question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'do' statements in modules do not get evaluated ?

See original GitHub issue

It 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
charlesroddiecommented, Sep 17, 2022

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”.

1reaction
keramscommented, Sep 17, 2022

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

module X

let _initializer: unit =
    oneTimeSideEffect ()

let funcRequiringOneTimeInitialization x y =
    _initializer // this is required, otherwise _initializer is never evaluated
    doStuff x y

It feels very brittle and unintuitive though. Yes, I can use a class with a static ctor for this, but “class bad, module good” 😃))).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found