Re: [Vala] valasemanticanalyzer error



On Fri, 2010-08-20 at 19:49 +0530, Martin DeMello wrote:
On Fri, Aug 20, 2010 at 7:26 PM, Andrea Del Signore <sejerpz tin it> wrote:

Thanks for the explanation but I can't really imagine any other way of
doing this without specializing the map method, because I don't see
implemented any "standard" (non generic) IList interface in gee.

So:

public void map<T>(DFunc fn, Gee.List<T> acc) { ... }

and then

a.map<string>((Enumerable.DFunc<int, string>) f , b);

Thanks! I didn't know you could have generic *methods* - is that
documented anywhere? 

Mmm... I didn't find any document that talks about generic methods, but
vala has such methods quite from the start IIRC.

If you want to use a lambda this is what I came with (see the map
function):

using Gee;

public interface Enumerable<G> : Iterable<G> {
  public delegate O DFunc<I, O>(I elem);

  public void map<I, T>(DFunc<I, T> fn, Gee.List<T> acc) {
    foreach (G i in this) {
      acc.add(fn(i));
    }
  }
}

public class EnumerableList<G> : Gee.ArrayList<G>, Enumerable<G> {
}

public string f(int i) {
  return "%d".printf(i);
}

public static int main(string[] args) {
  var a = new EnumerableList<int> ();
  a.add(1);
  a.add(2);
  a.add(3);
  var b = new Gee.ArrayList<string> ();
  a.map<int, string>(f , b);
  foreach (string i in b) {
    stdout.printf("-- %s --\n", i);
  }
  b.clear ();
  //using a lambda
  a.map<int, string>((i) => { return "result: %d".printf (i); } , b);
  foreach (string i in b) {
    stdout.printf("-- %s --\n", i);
  }

  return 0;
}

HTH,
        Andrea





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]