Re: Enumeration data type

"Brian Webb" <iRobot@swbell.net>
19 Jan 2001 23:15:00 -0500

          From comp.compilers

Related articles
Enumeration data type irobot@swbell.net (Brian Webb) (2001-01-18)
Re: Enumeration data type iRobot@swbell.net (Brian Webb) (2001-01-19)
Re: Enumeration data type tim.vanholder@falconsoft.be (Tim Van Holder) (2001-01-19)
Re: Enumeration data type kszabo@nortelnetworks.com (Kevin Szabo) (2001-01-19)
Re: Enumeration data type mike@dimmick.demon.co.uk (Mike Dimmick) (2001-01-19)
| List of all articles for this month |
From: "Brian Webb" <iRobot@swbell.net>
Newsgroups: comp.compilers
Date: 19 Jan 2001 23:15:00 -0500
Organization: FlashNet Communications, http://www.flash.net
References: 01-01-081
Keywords: types
Posted-Date: 19 Jan 2001 23:15:00 EST

Here's a more verbose example of what I'm trying to do. I use the
color code example.


    The following is just an example.
    Do not try and solve it as a problem.


Suppose I have a routine that accepts one of the following five color
codes as an argument.


    Black = 0
    Red = 1
    Green = 2
    Blue = 8
    White = 16


A sample call would look like this:


    Call Set_Background(Blue)


I don't want the following call to work, since the value used wasn't
pre-defined.


    Call Set_Background(7)


I'd prefer not to allow the following to work, even though the numeric
value is correct. I may forget what 8 means, or decide to modify the
color codes.


    Call Set_Background(8)


I could just define some constants...


    integer constant Black = 0
    integer constant Red = 1
    integer constant Green = 2
    integer constant Blue = 8
    integer constant White = 16


....or some typed constants,...


    datatype Color_Code is integer
    Color_Code constant Black = 0
    Color_Code constant Red = 1
    Color_Code constant Green = 2
    Color_Code constant Blue = 8
    Color_Code constant White = 16


....but someone might decide to scroll down a few pages and add their
own color codes for an unrelated routine.


    integer constant Purple = 6
    integer constant Grey = 15


or even


    Color_Code constant Purple = 6
    Color_Code constant Purple = 15


Then, someone might try


    Call Set_Background(Purple)


and wonder why it doesn't work.


So, what I'm trying to design is a construct that forces all of the
values of a data type to be declared in one place. I thought the
following looked OK, but I'm worried that using the term "enumeration"
may a poor word to use, especially for the non-integer data types.


    enumeration Color_Code is integer
        Black = 0
        Red = 1
        Green = 2
        Blue = 8
        White = 16
    end enumeration Color_Code


I was wondering if anyone has seen anything like this in an existing
language.


- Brian


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.