We only need to implement three things The collection class needs to implement an iterator() method that returns an object capable of iterating over your collection The iterator object needs to provide two methods: * bool next() * T get () where T is your element type --- vala will translate the foreach to something like var iter = yourcollection.iterator (); while (iter.next ()) { var element = iter.get (); ... your foreach loop body ... } if the collection class supports that, it should work with foreach
If we'd just inherit SimpleResultSet from the underlying GLib.GenericArray instead of just Object, we'd get this and a lot more for free.
SimpleResultSet is just an internal implementation of ResultSet, ResultSet needs to have the iterator interface, not SimpleResultSet.
Created attachment 65736 [details] [review] Add foreach support to ResultSet
Created attachment 65738 [details] [review] Add foreach support to ResultSet
Created attachment 65740 [details] [review] Add foreach support to ResultSet
Comment on attachment 65740 [details] [review] Add foreach support to ResultSet Review of attachment 65740 [details] [review]: ----------------------------------------------------------------- Looking great, just one thing: ::: test/direct/datamodel-test.vala @@ +25,5 @@ > { > Test.init (ref argv); > > Test.add_func ("/Datamodel/MatchesTemplate/anything", matches_template_anything_test); > + Test.add_func ("/Datamodel/MatchesTemplate/foreach", foreach_test); Please add one more test that runs foreach twice on the ResultSet instance.
Created attachment 65742 [details] [review] Add foreach support to ResultSet 2 An alternative implementation
Going with the second solution congrats to all
Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.