Heres the enum proposal I was getting at for cobra.
This is addressing 2 issues with the current implementation
- testing Enum set value inclusion (using bit operations) is obscure, error prone,
non obvious (if not have a good low level coding background) and implementation dependent.
- Bit operations on Enums on some(one so far) backends are totally unsupportable.
What I would like to do is move the abstraction for these ops up a level to make the above problems disappear
AND make it more obvious what operations you are trying to do.
which is clearer?
if today in weekend, print 'go biking'
if today & weekend == today, print 'take a hike'
# whats the difference if I do this
#if weekend & today, print 'take a hike'
# or this
#if weekend | today, print 'everday take a hike'
So basically the proposal is as follows:
- Keep the current declaration and initialisation syntax for Enums
(keep the current setting of Flags Attribute for .Net to indicate use of explicit non colliding bitpattern values)
- perhaps lose the storageType specification capability - its unsupportable/ignored on java anyway
Is that used much on .Net?
- Deprecate/remove the bitwise operations on Enums in favor of only direct comparison (==) and 'in' testing
(which replaces the bitset twiddling).
This 'virtualises' the testing operation away from any int based implementation and the need for
explicit bit twiddling operations but doesnt preclude them being done for supporting a BE implementation (i.e at codegen)
Enum DayOfWeek
Mon, Tues, Wed, Thur, Fri, Sat, Sun
...
today = DayOfWeek.Mon
alsoToday = DayOfWeek(Mon)
assert today == DayOfWeek.Mon
assert alsoToday == today
...
# test explicit value
if today == DayOfWeek.Mon
print 'its Monday'
# groups/sets of enums
weekend = DayOfWeek(Sat, Sun)
workday = DayOfWeek(Mon, Tues, Wed, Thurs, Fri)
#Test inclusion
if today in weekend
print 'go biking'
#if weekend & today == today
# existing syntax test becomes error/unsupported
# transition generate a warning noting the new syntax
# 'Cobra does not use C# syntax for testing enum set inclusion use "today in weekend" instead"
if today in weekday
print 'hi ho hi ho, Its to pay a mortgage I go'
if alsoToday not in DayOfWeek(Mon, Tues, Wed)
print 'nearly the weekend'
At the higher cobra compiler level theres no change apart from losing/deprecating binary Ops on Enums
The only code impact is at codegen (SharpGenerator and JavaGenerator)
.Net conflates Enums and (Bit)sets of Enums into (explicitly set) binary valued Enums.
No change here re representation (still a .Net enum)
just generate the right bit twiddling code for 'enum in Enum(Set)'
(rewrite cobra
'e in E' to C#
'e & E == e' )
Other platforms (that use true symbolic Types for Enums - Java) have to distinguish (at codegen) between uses of
single valued and sets of Enum values and generate the right BE type (done).
The Set inclusion testing operation is done as a mapping to the BE types ( bitset test)
The advantage is that at the cobra level we have a single, clear (always working) high level syntax for enum value
testing that can be easily detected and mapped to the right operation set for the backend in use.
The alternative is to provide/allow a number of testing ops at differing levels of abstraction
(bit twiddling and 'in' and ..)
that different backends support or ignore/errorEmit or try to remap to varying levels of completeness.