Re: [Vala] valasemanticanalyzer error



On Thu, Aug 19, 2010 at 6:39 PM, Jürg Billeter <j bitron ch> wrote:
On Tue, 2010-08-17 at 18:09 +0530, Martin DeMello wrote:
public interface Enumerable<G, T> : Iterable {

      public delegate T DFunc(G elem);

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

This is invalid code. Inner types don't have access to generic type
parameters of outer types. So you probably want something as follows:

public interface Enumerable<G, T> : Iterable {

       public delegate T DFunc<G, T>(G elem);
       [...]
}

Okay, I made that change, and now I'm getting the same error that .92 gives:

test.vala:23.37-23.37: error: Argument 1: Cannot convert from `G' to `int'
        a.map( (i) => { return "%d".printf(i); }, b);
                                           ^
i.e. it cannot infer the type of i even though a is of type
EnumerableList<int, string>

Also, does "support for generic delegates" mean that I will ultimately
be able to specialise the delegate function when I call it? Something
like this at the least, though anonymous function support would be
nice:

using Gee;

public interface Enumerable<G> : Iterable {

        public delegate T DFunc<G, T>(G elem);

        public void map(DFunc fn, Gee.List acc) {
                foreach (G i in this) {
                        acc.add(fn(i));  // <--  error: missing generic type arguments
                }
        }
}

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((Enumerable.DFunc<int, string>) f , b);
        foreach (string i in b) {
                stdout.printf("-- %s --\n", i);
        }

        return 0;
}

martin



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