-
Notifications
You must be signed in to change notification settings - Fork 847
Closed
Labels
Description
Given C# code with a const (and readonly for comparison):
public class Class1
{
public const string MyConst = "my const";
public static readonly string MyReadonly = "my readonly";
}F# compiler allows assignment to the const (leaving it to fail at runtime):
Class1.MyConst <- "changed" // compiler allows this
Class1.MyReadonly <- "changed" // FS0690: This field is readonlyNote that C# compiler disallows:
Class1.MyConst = "changed"; // CS0131: The left-hand side of an assignment must be a variable, property or indexer
Class1.MyReadonly = "changed"; // CS0198: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)Repro steps
Create C# class library project with:
namespace ClassLibrary1
{
public class Class1
{
public const string MyConst = "my const";
public static readonly string MyReadonly = "my readonly";
void Test()
{
// Uncomment for C# compiler errors
// Class1.MyConst = "changed";
// Class1.MyReadonly = "changed";
}
}
}Create F# project referencing above C# project, with:
open ClassLibrary1
Class1.MyConst <- "changed" // compiler allows this
Class1.MyReadonly <- "changed" // FS0690: This field is readonlyExpected behavior
F# compiler should prevent assignment to const at compile time.
Actual behavior
F# compiler allows assignment to const at compile time.
Related information
- Operating system: Windows 10
- .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET Core 3.1.302
- Editing Tools (e.g. Visual Studio Version, Visual Studio): Visual Studio 2019 16.6.5
abelbraaksma and brettrowberry