rfc:enum

This is an old revision of the document!


Request for Comments: Enum

This RFC is about adding the enum language structure

Introduction

When writing a program, it is often required to create lists of constants representing integers to make the code more readable. This RFC introduce a new language construct to define such a list by assigning unique arbitrary values or not.

Why do we need enum

Frequently developers need to produce code like this:

const	LOG_LEVEL_DEBUG = 1,
	LOG_LEVEL_INFO = 2,
	LOG_LEVEL_WARNING = 3,
	LOG_LEVEL_ERROR = 4;
 
// Or
 
class Tokens {
	const	T_IF = 258,
		T_ELSE = 259,
		T_WHILE = 260,
		T_DO = 261;
}

The proposal is that this could be written in a much more concise manner:

enum { 
	LOG_LEVEL_DEBUG,
	LOG_LEVEL_INFO,
	LOG_LEVEL_WARNING,
	LOG_LEVEL_ERROR 
};
 
// Or
 
class Tokens {
	enum { 
		T_IF = 258,
		T_ELSE,
		T_WHILE,
		T_DO 
	};
}

Proposal and Patch

Syntax: “enum” “{” name [ “=” intvalue ] [, name [ “=” intvalue ] ] … “}”

Where :

  1. name is the name of the constant to define
  2. intvalue is
    1. the value of this constant
    2. if not defined the value will be the value of the previous defined constant in the same enum + 1
    3. if there is no previous defined constant in the same enum the value will be one
rfc/enum.1302087566.txt.gz · Last modified: 2013/04/04 18:08 (external edit)