Fsharp: Unexpected Compiler Errors with Nullable Value Tuples

Created on 20 Jun 2018  路  3Comments  路  Source: dotnet/fsharp

Usage of struct tuples (struct (0,0)) with the System.Nullable<T> type seems to cause unexpected compiler errors with F#. When attempting to directly instantiate a Nullable(struct (0,0)), the error A generic construct requires that the type 'struct (a * b)' have a public default constructor is reported. When attempting to instantiate a Nullable using a struct tuple bound to an identifier, such as Nullable(x), the error internal error: tcrefOfAppTy (Failure) is thrown. Simply unboxing the struct tuple as a ValueTuple<int,int> seems to resolve both of these errors.

Repro steps

  1. Execute the following code in F# interactive:
let x = System.Nullable(struct (0, 0))

This produces the error: error FS0001: A generic construct requires that the type 'struct ('a * 'b)' have a public default constructor

  1. Execute the following code in F# interactive:
let x = struct(0, 0)
System.Nullable(x)

This produces the error error FS0073: internal error: tcrefOfAppTy (Failure)

  1. Execute the following code in F# interactive:
let x = struct(0, 0)
let y = System.Nullable(struct (0, 0) |> unbox<System.ValueTuple<int,int>>)
let z = System.Nullable(x |> unbox<System.ValueTuple<int,int>>)

This executes without errors.

Expected behavior

Creating instances of Nullable with a struct tuple should work without error, equivalent to the behavior for using System.ValueTuple.

Actual behavior

Different compiler errors are reported when attempting to instantiate a Nullable with a struct tuple.

Known workarounds

Unboxing the struct tuple to a System.ValueTuple resolves the compiler errors and executes as expected.

Related information

Please see this StackOverflow post for the original report of this behavior.

Windows 10
.NET Framework 4.7.1
Visual Studio 2017

Area-Compiler Resolution-Duplicate bug

All 3 comments

Accidentally confirmed that this is still an issue.
I just stumbled upon this issue in F# 4.7.0.0.
I consider it a major hickup for C# F# interop.

Visual F# Tools 10.9.1.0 for F# 4.7 16.6.0-beta.20217.4+1c969cac25e2d38d71872efe6c8226029e42bb59
Microsoft Visual F# Tools 10.9.1.0 for F# 4.7

Though there's a simple workaround: create an explicit struct instead.
e.g. instead of defining the function signature like:

abstract (string Name, int Value)? GetStuff();

do it like this:

struct Stuff
{
    public string Name;
    public int Value;
}

abstract Stuff? GetStuff();
override this.GetStuff() =
    Nullable(Stuff(Name="Answer", Value=42))

Just to note that the internal compiler error is not present anymore, and is instead:

image

Treating as duplicate of #7618

Was this page helpful?
0 / 5 - 0 ratings