Foreach func on any iterable struct

class SmplArrWrap<T>: ??? {
  public Iterable<T> itr;

  void print_arr(){
    foreach (var a in itr){
      print(@"$a\n");
    }
  }
}
//to_string' does not exist in the context of 'T'

I want to create a function that takes any Iterable and print it with foreach. For example Python or JS can write massive if u just put it in print() func, wanna the same for Vala.

But I ran into two problems. First of all, all the base types in Vala are just structs that are not inherited from a common ancestor, so I don’t know how to pass .to_string capability to T to output in template strings @"$T". I would also like to note that GLib.Object also does not contain to_string, although in C# it has ToString() which outputs the object type if the object is not of the base type.

Secondly, I didnt find the Iterable interface in Vala, it is in Gee, but I want it to work with normal arrays.

Vala depends on GLib for its runtime library and type system. There was an attempt to add an iterator API to GLib, but it didn’t happen: https://wiki.gnome.org/Attic/IteratorsAPI

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.