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
Recognising [<System.Runtime.CompilerServices.ModuleInitializer>] #1024
Comments
That's per class, right? |
@kerams per project. |
This is what C# spits out. namespace Preview
{
class Test
{
[System.Runtime.CompilerServices.ModuleInitializer]
public static void Init()
{
}
[System.Runtime.CompilerServices.ModuleInitializer]
public static void Init2()
{
}
}
}
Basically just a static ctor for |
Yes, this is what module initializers do. |
F# has on-demand file-level initialization, which is necessary for coherent, safe initialization of 'let' bindings. It is a subtle mechanism built out of existing .NET initialization mechanisms that is very, very carefully implemented and tested. I suspect adding DLL/whole-assembly initialization will interact exceptionally badly with this mechanism, exposing the programmer to all sorts of situations where static 'let' bindings are not yet established. Note F# doesn't really let you write class initializers explicitly (the class initializers do exist but they get chained into file-level initialization code, with the exception of generic types, which are called out as a special case in the F# spec). That said, if a .NET DLL/whole-assembly initialization mechanism had existed in .NET 2.0 we probably would have made use of it for F# initialization. In short, F# takes its own view on initialization because we want static |
But module initializers in IL has existed since .NET Framework 1.0 (see ECMA-335, 1st edition, December 2001, § II.9.8). |
Closing this as we don't plan to support this form of initialization. |
I propose we recognize the attribute for the C# 9.0 feature: Module Initializers.
The existing way of approaching this problem in F# is to use tools for injecting IL post-build.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantage of making this adjustment to F# is that the name of (IL) module initializers may be conflated with (F#) modules. However, this name was chosen by the C# team and will be confusing if this attribute does not work similarly in F#.
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
S if we limit the use of this attribute to one static method only,
M if we implement the full C# feature where multiple static methods can have this attribute, and a deterministic order is produced.
Related suggestions: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: