使用 Nemerle,我想创建一个具有泛型类型约束和“需要”前置条件的方法。关于方法的返回类型,这些的正确顺序/语法是什么?
这是我想要的 C# 示例:
//Count the number of items in a 2D array
public static int CountAll<T>(this T[] source)
where T : Array
{
Contract.Requires(source != null);
return source.Sum(sub => sub.Length);
}
这是我最好的 Nemerle 猜测,编译器不喜欢它:
public static CountAll[T](this source : array[T]) : int
where T : array
requires source != null
{
source.Sum(sub => sub.Length);
}
我怎样才能使这个编译?
编辑:
我实际上是在使用array
关键字System.Array
.
这编译:
public static CountAll[T](this source : array[T]) : int
where T : System.Array
requires source != null
{
source.Sum(sub => sub.Length);
}