[nem-en] use extension methods from derived class
Kamil Skalski
kamil.skalski at gmail.com
Mon May 8 20:54:42 CEST 2006
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 at work.email.ne.jp> 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 at work.email.ne.jp>
>
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
>
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list