.net - 修复了 "A type parameter is missing a constraint ' 时非常可怕的 'N : equality'“错误

标签 .net f# constraints

这段代码:

open System
open System.Collections.Generic

type INode =
    interface
        inherit IEqualityComparer<INode>
        inherit IEquatable<INode>
    end

let myFun<'N when 'N :> INode> (n:'N) = n = n

生成以下构建错误:

A type parameter is missing a constraint 'when 'N : equality'   

我知道我可以在函数级别安抚编译器

let myFun<'N when 'N :> INode and 'N : equality> (n:'N) = n = n

但我更愿意让类型约束保持在 myFun 中,并在 INode 中“修复”问题,以便在我遇到问题时更容易希望多个函数具有相同的约束。

可能吗?

最佳答案

您不需要 <> 内的位删除后问题就会消失:

open System
open System.Collections.Generic

type INode =
    interface
        inherit IEqualityComparer<INode>
        inherit IEquatable<INode>
    end

let myFun (n:'N when 'N :> INode) = n = n

一个更简单的版本(来自@ben)是使用可向下转换的类型的简写,如下所示:

let myFun (n: #INode) = n = n

关于.net - 修复了 "A type parameter is missing a constraint ' 时非常可怕的 'N : equality'“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21517511/

相关文章:

.net - Outlook.com的API

c# - 接口(interface)优化代码?

c# - 在对象列表中查找对象数据重复项

c# - 并发访问 .NET 中的静态成员

recursion - 在 F# 中使用免费 monad 是否意味着更长的启动时间和有限的指令?

f# - 区分联合列表 F# 的两个值的总和

object - F# 中的内联 C# 对象创建

ios - Swift 中心 View 水平比例

xcode - 试图在 swift 中为约束设置动画

sql-server - 仅当 Microsoft sql server 中的 t1.f3 为空时,如何允许字段 t1.f1 引用 t2.f2?