Pattern for enumerations? (Attn: Roger Browne) 
Author Message
 Pattern for enumerations? (Attn: Roger Browne)

  I have attached below some code which shows the use of enumerated
types in Eiffel.  It works on a similar principle to how, say,
integers themselves might be handled: Out there, somewhere, are
all the integers, and when we say that X is 5, that means that the
variable X refers to the object 5.

  Similarly, I create objects representing the values in the
enumeration, and the actual enumerated-type variables are references
to these objects.

  The test program included did not report any errors.  I'm open
to any comments anyone would like to provide.

 -PD

---- CAT: COLOURE.E

class COLOUR_ENUM
        -- Mixin provides values for dealing with COLOUR enumerated type

feature {ANY}

    frozen red : COLOUR is
        once
            !!Result
        end

    frozen green : COLOUR is
        once
            !!Result
        end

    frozen blue : COLOUR is
        once
            !!Result
        end

    frozen is_valid_colour (param : COLOUR) : BOOLEAN is
        do
            Result := (
                param = red or
                param = green or
                param = blue
            )
        end

end

---- CAT: done COLOURE.E (32 lines)
---- CAT: COLOUR.E

class COLOUR

inherit
    COLOUR_ENUM
    end

creation {COLOUR_ENUM}

invariant
    valid_colour: is_valid_colour (Current)    

end

---- CAT: done COLOUR.E (14 lines)
---- CAT: COLTEST.E

class COLTEST
        -- Test class to see of the COLOUR enumeration works

inherit
    COLOUR_ENUM
    end

creation {ANY}
    make

feature {NONE}

    io : BASIC_IO

    make is
        local
            A, B : COLOUR
        do
            !!io
            io.put_string ("Checking for errors...")

            A := red
            B := blue
            if A = B then
                io.put_string ("Error 1: red = blue")
            end
            if A /= red then
                io.put_string ("Error 2: red /= red")
            end

            A := B
            if A /= B then
                io.put_string ("Error 3: blue /= blue")
            end

            io.put_string ("Done.")
        end

end

---- CAT: done COLTEST.E (41 lines)

--
--
Patrick Doyle



Thu, 13 Jan 2000 03:00:00 GMT  
 Pattern for enumerations? (Attn: Roger Browne)



Quote:
>   I have attached below some code which shows the use of enumerated
> types in Eiffel.  It works on a similar principle to how, say,
> integers themselves might be handled: Out there, somewhere, are
> all the integers, and when we say that X is 5, that means that the
> variable X refers to the object 5.

>   Similarly, I create objects representing the values in the
> enumeration, and the actual enumerated-type variables are references
> to these objects.

[colour example snipped]

Interesting.  I've often pondered this one myself and come up with the same
solution as the most natural implementation in an OO language.  It
especially comes into its own when you want to add methods to colour
(GetRGBValues...).  It is, however, extremely "wordy" which could represent
a significant barrier to using this pattern in languages which support an
enum keyword.  I wonder if there is a case for adding language support i.e.
to use a keyword like enum as a kind of short hand for your pattern?

Have you considered how to define and iterate over colour sub-ranges/sets
(use of standard containers, iterators; addition of First, Last, Next, Prev
methods to colour class??)

--
Gavin Collings



Sat, 15 Jan 2000 03:00:00 GMT  
 Pattern for enumerations? (Attn: Roger Browne)


Quote:



>>   I have attached below some code which shows the use of enumerated
>> types in Eiffel.  It works on a similar principle to how, say,
>> integers themselves might be handled: Out there, somewhere, are
>> all the integers, and when we say that X is 5, that means that the
>> variable X refers to the object 5.

>>   Similarly, I create objects representing the values in the
>> enumeration, and the actual enumerated-type variables are references
>> to these objects.

>[colour example snipped]

>Interesting.  I've often pondered this one myself and come up with the same
>solution as the most natural implementation in an OO language.  It
>especially comes into its own when you want to add methods to colour
>(GetRGBValues...).  It is, however, extremely "wordy" which could represent
>a significant barrier to using this pattern in languages which support an
>enum keyword.  I wonder if there is a case for adding language support i.e.
>to use a keyword like enum as a kind of short hand for your pattern?

  Yes, the wordiness is a problem.  I would like to see an enum keyword.

Quote:
>Have you considered how to define and iterate over colour sub-ranges/sets
>(use of standard containers, iterators; addition of First, Last, Next, Prev
>methods to colour class??)

  No, I haven't.  Do you have any thoughts on this?  Should Colour be
a subclass of Container?

 -PD
--
--
Patrick Doyle



Sat, 15 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Thanks to Roger Browne

2. Thank you Roger Browne!

3. Good SOAP lib for Python 2.2 (Christopher Browne)

4. pattern,pattern problem

5. Pattern Definition GREP with Pattern Editing for MSDOS

6. How to write filesearch with begin-pattern till end-pattern

7. NYSIGAPL presents Ken Iverson & Roger Hui

8. APL BUG, the N. Cal SIGAPL of the ACM presents Roger Hui

9. Roger Hui in Bay Area Info?

10. Yet another message from Roger Hui

11. Replies from Roger Hui

12. J: Bubble Sort (2) Roger&Ken

 

 
Powered by phpBB® Forum Software