Question

For example, if I have a record such as:

type sample = { userId : string; }

and then I define this record in C# such as:

var samp = new sample()
                    {
                       userId = null;
                    };

and then if I assign this record to an F# function called in C# what would happen? Is this legal? Would I just simply have to check for null within the F# function, or is this not possible?

Was it helpful?

Solution

Yes, in general, any reference type may be null. The F# compiler prevents null assignment to F#-specific types such as records and discriminated unions in F# code. However, it is still possible to assign null to variables of these types from C#.

String is a reference type and may be null, even from F#. Your C# code is slightly inaccurate in that the type sample would have userId in the constructor:

var samp = new sample(null);

If you annotate the sample type is CLIMutable attribute then it will also have a parameterless constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top