Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I match a list of .NET type for null in a F# record type?

I have a class defined in C#:

public class Foo
{
    public string Bar { get; set; }
}

Then I define a record type in F# with a method returning a new object:

type Bar =
    {
        FooList: Foo list
    }
    member this.FromBarList(barList: string list) =
        let fooListNotNull =
            match this.FooList with
            | [] -> []
            | _ -> this.FooList |> List.filter (fun x -> (List.contains x.Bar barList ) )
        {
            FooList = fooListNotNull
        }

Since the type 'Bar' will be used in C# code, the property FooList can be null, and I'd like to check for it. But I'm getting the error:

The type 'Foo list' does not have 'null' as a proper value

Despite I find this type of matching in docs: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/values/null-values

How can I match null correctly?

like image 947
Phate01 Avatar asked Nov 20 '25 02:11

Phate01


1 Answers

It is possible to make a null F# list within F# and presumably in C# too:

let nullList = Unchecked.defaultof<int list>

You can check for null but you need to box it first:

match box nullList with
| null -> []
| _ -> nullList

Here's a generic function to do this for any value:

let defaultNull defaultValue x =
    match box x with
    | null -> defaultValue
    | _ -> x

This is how you would use it in your case:

nullList |> defaultNull []
like image 107
TheQuickBrownFox Avatar answered Nov 22 '25 16:11

TheQuickBrownFox