Home > Back-end >  Is the mapping between C# nullable types and the F# option type recommended?
Is the mapping between C# nullable types and the F# option type recommended?

Time:12-29

Given the following F# record type:

type TestRecord =
    { CustomerId: option<int>
      Email: option<string> }

I can create instances of this type from C# with

int? customerId = null;
string email = null;

var tr = new TestRecord(customerId, email);

If I process the record in F# coming this way from C#, each null C# value becomes a None on the F# side and non-null C# values become Some x on the F# side.

This automatic conversion from C# nullables to F# options is very convenient for the purpose I'm planning to use it. However, I'm unclear on whether this is one of those things that works but is not recommended. I've searched for some docs on this aspect of C# to F# interop but can find a definitive answer.

Is this automatic conversion behavior recommended?

CodePudding user response:

As per the F# Language Specification 18.4.2 Option.None is represented as null (and Some x as x), so it should be safe to rely upon.

  • Related