logo       

use extension methods from derived class: msg#00008

lang.nemerle.devel

Subject: use extension methods from derived class

Hi

there is the following extension methods.

module Extensions {
public All [T] (this objs : IEnumerable[T], fn : T -> bool) : bool {
return : {
foreach (o in objs)
when (!fn (o))
return (false);
return (true);
}
}
}

and use this.

def ls = $ [0, 2 .. 10];
def ar = array [1, 2, 3, 4, 5];

ls.All (x => x % 2 == 0);
ar.All (x => x < 10);

but compile error occurred.

however,

def ls = $ [0, 2 .. 10] : IEnumerable [int];
def ar = array [1, 2, 3, 4, 5] : IEnumerable [int];

then compile ok.


C#3.0 (LINQ) do not need that cast.

-- sample.cs for C#3.0

using System;
using System.Collections.Generic;

public delegate bool Pred<T>(T t);

public static class Extensions {
public static bool All<T> (this IEnumerable<T> objs, Pred<T> pred)
{
foreach (T o in objs)
if (!pred(o))
return false;
return true;
}
}

public class App {
public static void Main () {
int[] arr = new [] { 2, 4, 6 };
if (arr.All<int>(delegate (int x) { return x % 2 == 0; }))
Console.WriteLine ("All !");
}
}

I'd like to do this.

--
akiramei <mei@xxxxxxxxxxxxxxxx>



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise