【问题标题】:Bug in F# 2.0 wrt to where constraints in generic type parameters?F# 2.0 中的错误到泛型类型参数的约束在哪里?
【发布时间】:2012-08-20 15:25:39
【问题描述】:

如果我有一个想从 F# 使用的 C# 方法并接收两个这样的类型化参数:

public class Foo
{
    public static void GenericMethodWithTwoTypeParamsThatHaveAWhereClass<TA, TB>() where TA : TB
    {

    }
}

当我尝试通过 F# 调用它时,编译器会报错:

Foo.GenericMethodWithTwoTypeParamsThatHaveAWhereClass<System.IO.BinaryWriter, System.IDisposable>()

那么,这是 F# 2.0 中的错误吗?

【问题讨论】:

标签: c# generics f# c#-to-f# f#-3.0


【解决方案1】:

F# 不支持这种类型的约束。 spec 表示t1 :&gt; t2 形式的约束被视为t1 = t2,这解释了您的错误:

这个表达式应该是 BinaryWriter 类型,但这里是 System.IDisposable 类型

根据TB 的使用方式,您也许可以使用

GenericMethodWithTwoTypeParamsThatHaveAWhereClass<BinaryWriter, _>()

根据上述规则,TB 将被推断为BinaryWriter。但是,如果这样可行,那么您的 C# 方法很可能只使用一种类型参数。

【讨论】:

  • 你知道这是否在 F# 3.0 中修复了吗?
  • 这是设计使然...并且在 3.0 中保持不变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多