dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.87k stars 783 forks source link

System.Nullable not supported in `sortBy` in `query`/ linq computation expressions #11446

Closed smoothdeveloper closed 3 years ago

smoothdeveloper commented 3 years ago

The following C# works out of the box

using System;
using System.Collections.Generic;
using System.Linq;
public record Data(DateTime? date);
public class C {
    public IEnumerable<Data> M() {
        var datum = new []{new Data(DateTime.Now),new Data(null)};
        var result = from d in datum orderby d.date select d;
        return result;
    }
}

I'd like the same to work in F#

open System
type Data = {date: DateTime Nullable }
let datum = [|{date = Nullable DateTime.Now}; {date = Nullable()}|]
let q = query {
    for d in datum do
      sortBy d.date
      select d
  }

but it gives:

error FS0001: The type 'Nullable' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface

If it doesn't come as default, is it something that can be extended by overloading SortBy implementation or should be added to FSharp.Core?

https://github.com/dotnet/fsharp/blob/830a411cbad94d128fe40cedd2a24a69ccda35a6/src/fsharp/FSharp.Core/Query.fs#L216-L217

smoothdeveloper commented 3 years ago

I now see sortByNullable below in the code is a separate function and also in the query expr doc, I guess that's the way to do it if it can't be an overload.