GNOME Bugzilla – Bug 624691
Improve Enum: add foreach capabilites
Last modified: 2013-01-04 11:54:40 UTC
It would be great if it were posible to use foreach statement in Enum blocks, as seen in this example: public enum MyEnum { A, B, C, D, E } bool lookup_whatever() { foreach(MyEnum elem in MyEnum) { if(matrix[elem]==5) return true; } }
Agreed, that would be handy in some situations. Obviously there is a simple workaround by adding a MAX_VALUE and iterating over an int, still foreach support would be nice as syntatic sugar.
Created attachment 207054 [details] [review] Proposed Patch With this patch valac generates a ".all_values" constant to enums. It can be used with foreach like this: foreach (Enum e in Enum.all_values) { stdout.printf ("%s\n", e.to_string ()); } If one wants support for "foreach(Enum e in Enum)" this, or a similar method is needed to avoid the gap problem (e.g. enum Example { FOO = 1, BAR = 3 };).