logo       

Re: use extension methods from derived class: msg#00009

lang.nemerle.devel

Subject: Re: use extension methods from derived class

Probably some special handling is needed at the point between array[T]
and IEnumerable[T], as they are related as special case not by
definition in BCL.
Thanks for pointing that, we try to fix this soon.

On 5/8/06, mei <mei@xxxxxxxxxxxxxxxx> wrote:
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>

_______________________________________________
https://nemerle.org/mailman/listinfo/devel-en



--
Kamil Skalski
http://nazgul.omega.pl


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

News | FAQ | advertise