F#泛型泛型构造要求类型'struct(Guid*int)'具有公共默认构造函数

Mic*_*hal 6 generics f# c#-to-f# valuetuple

我在C#中有一个带有此返回类型的方法的接口:

Task<(Guid, int)?>
Run Code Online (Sandbox Code Playgroud)

我需要在F#中实现这个接口,如果我没有弄错,这在F#中应该是等价的:

Task<Nullable<ValueTuple<Guid, int>>>
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我编译时,我收到此消息:

generic construct requires that the type 'struct (Guid * int)' have a public default constructor
Run Code Online (Sandbox Code Playgroud)

我发现了一些类似的问题,看起来解决方案是使用[<CLIMutable>]属性.但这不是我可以用System.ValueTuple做的事情.有没有办法在F#中使用Nullable ValueTuple?

Aar*_*ach 8

我认为这是F#中的编译器错误.您可能希望在F#GitHub页面上打开一个问题并报告此行为.我怀疑它是一个编译器错误的原因是我可以通过简单地拆箱struct (System.Guid, int)来使它工作ValueTuple<System.Guid, int>,它应该已经是等效的类型.以下是我在一个示例中使用它的方法,这也可以作为一个可行的解决方法,直到有更熟悉F#编译器的人可以告诉您它是否是真正的错误:

open System
open System.Threading.Tasks

type ITest =
    abstract member F: unit -> Task<Nullable<ValueTuple<Guid, int>>>

type T () =
    interface ITest with
        member __.F () =
            let id = Guid.NewGuid()
            let x = Nullable(struct (id, 0) |> unbox<ValueTuple<Guid, int>>)
            Task.Run(fun () -> x)

(T() :> ITest).F().Result
Run Code Online (Sandbox Code Playgroud)