JamesFaix
JamesFaix

Reputation: 8655

Nemerle - How do I write a method signature with a generic constraint and "requires" condition?

Using Nemerle, I want to create a method that has a generic type constraint and a 'requires' pre-condition. What is the proper order/syntax for these, with respect to the return type of the method?

Here is a C# example of what I want:

//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);
}

Here is my best Nemerle guess, which the compiler does not like:

public static CountAll[T](this source : array[T]) : int
    where T : array
    requires source != null
{
    source.Sum(sub => sub.Length);
}

How can I make this compile?


EDIT:

I was using the array keyword when I actually meant System.Array.

This compiles:

public static CountAll[T](this source : array[T]) : int 
    where T : System.Array
    requires source != null
{
    source.Sum(sub => sub.Length);    
}

Upvotes: 2

Views: 63

Answers (0)

Related Questions