Bug 624691 - Improve Enum: add foreach capabilites
Improve Enum: add foreach capabilites
Status: NEW
Product: vala
Classification: Core
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2010-07-18 22:34 UTC by Pau Espin Pedrol
Modified: 2013-01-04 11:54 UTC (History)
4 users (show)

See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed Patch (1.71 KB, patch)
2012-02-08 06:43 UTC, Sebastian Reichel
none Details | Diff | Review

Description Pau Espin Pedrol 2010-07-18 22:34:32 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;

}


}
Comment 1 Michael 'Mickey' Lauer 2011-03-03 15:40:44 UTC
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.
Comment 2 Sebastian Reichel 2012-02-08 06:43:30 UTC
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 };).

Note You need to log in before you can comment on or make changes to this bug.