Wiki

Ticket #267 (new defect)

Opened 14 years ago

Last modified 12 years ago

Cobra Enums not support methods of Enum Baseclass

Reported by: hopscc Owned by:
Priority: minor Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: Cc:

Description

The .Net  Enum baseclass have a few none static methods and a plethora of static methods for obtaining member names and values and doing conversions.
Cobra Enums dont recognise these methods.

#access to Enum methods via cobra enum

class EnumMethods
    
    enum Days 
        Mon,Tues,Wed, Thurs, Fri, Sat, Sun
    
    enum States
        has Flags   
        None=0, Invalid=1, Settling=2, Valid =4 
        
    def main is shared
        d = Days.Mon
        weekend = Days(Sat, Sun)
        inweekend = d in Set<of Days>([Days.Sat, Days.Sun])
        print '[d] in weekendSet', inweekend
        
        #static
        print Enum.getName(Days, d)
        print Days.getName(Days, d) # shd work doesnt compile

        print Enum.getNames(Days)
        print weekend
    
        s = States()
        print Enum.getNames(States)
        print Enum.getValues(States)
        assert s inherits Enum
        print s.getNames(States) # shd work doesnt compile
        print s.getValues(States) # shd work doesnt compile

        print s.hasFlag(States.Invalid) # doesnt compile
        
        s = States.Settling
        s1 = s to Enum  # spurious warning
        print s1.hasFlag(States.Invalid)  # cant see hasFlag
        print s1.typeOf
    


Change History

Changed 14 years ago by Charles

Test comment.

Changed 14 years ago by Charles

I thought I replied to this before, but don't see it... oh, well.

On a related note, should we allow methods and properties on enums? These would be utilities like .isWeekend and such.  Java enums allow methods and even custom constructors. Although they also allow arbitrary state (any number of field members) which probably wouldn't fit in with .NET enums and is debatable on its own anyway.

Changed 14 years ago by hopscc

Dont see much point. Can get same effect with a class if needed

Changed 12 years ago by hopscc

To support this need to make EnumDecl class more like a (or subclassed off)
Cobra Primitive.
i.e hold component _box for supported methods and be wired to a nativeType and systemTypeProxy of 'enum'
Then have to provide a backend nativeType (or mapping) for 'enum'
same as do currently for existing primitive System Types ( 'bool'-> Bool, 'char'-> Char, int<size> -> IntegerTypes, etc)

Not clear how well ( or even how useful) such would be for different backends ( with other notions of what an Enum actually is).

Other option is to wire some subset of useful methods to the cobra EnumDecl and
map them ( if possible) to the backends enum support...

Perhaps better to just raise the abstraction level for cobra Enums.

Changed 12 years ago by hopscc

  • priority changed from medium to minor
Note: See TracTickets for help on using tickets.