-
Notifications
You must be signed in to change notification settings - Fork 773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'do' statements in modules do not get evaluated ? #13905
Comments
This would explain some strange initialization behavior I saw a couple of weeks ago. Thought I was going crazy. |
I think it's by design due to how static members are processed in compiler and fsi. |
If it is by design, will |
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 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" :)))). |
It would seem reasonable to warn when unit-valued ("do") initializers are used inside modules with no "observable initializers", implying that the unit-valued initializers will never be executed.
The module behaviour, while brittle, is well-defined, whereas class static dos apparently only run sometime before the class is used. In your case that is fine, so in your case class would be better and give more easily understandable behaviour. |
Yes, but it is well defined by compiling into a hidden class with a static constructor. So, whatever applies to the static constructor (first access kicks it), applies to the
|
It seems that
do
statements in modules do not get evaluated when the module is accessed from another assembly.Creating an empty project
then changing the code to
and calling it from fsi
The
do
statement was not evaluated.Whereas putting it all in fsi
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?The text was updated successfully, but these errors were encountered: