5

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

Task<(Guid, int)?>

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

Task<Nullable<ValueTuple<Guid, int>>>

不幸的是,当我编译时,我收到了这条消息:

generic construct requires that the type 'struct (Guid * int)' have a public default constructor

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

4

1 回答 1

7

我认为这是 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
于 2018-06-19T18:09:04.530 回答