泛型与F#中的接口 [英] Generics with interfaces in F#

查看:111
本文介绍了泛型与F#中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,可以声明泛型参数必须实现如下的特定接口:

  public class Something< T> T:IComparable 
{
...
}

如何在F#中指定它?

解决方案

通用约束在F#中使用when

 输入Foo<'a when a:> IComparable的> = 
会员x.Bla = 0


In C# one can state that a generic parameter must implement a certain interface like so:

public class Something<T> where T : IComparable
{
    ...
}

How does one specify this in F#?

解决方案

Generic constraints use "when" in F#:

type Foo<'a when 'a :> IComparable> = 
  member x.Bla = 0

这篇关于泛型与F#中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆