Enums

Antril enums - integer enums, enums over str and over value types, bitflag enums with declbits and allbits, member values, conversions, printing and the bitflag helpers.

Last updated 5 min read Edit on GitHub

On this page

An enum is a closed set of named values over a base type:

[attributes] [access] enum Name [: Base] { Member [= value], … }
[attributes] [access] bitflag enum Name [: Base] { Member [= value], … }
Base Members are Default
An integer type (i8 … u256) numbers i32 for enum
str strings
A value type values of that type
An unsigned or signed integer, for bitflag enum bits u32

A float, bool or a class is not a base (AN1006). An enum has one base (AN1063) and implements no interfaces. The members are separated by commas; a comma after the last one is allowed.

Integer enums

AntrilRun
import System;

public enum Level : u8 { Low = 1, Mid, High = Low * 10, Top }

Main()
{
	Level l = Level.Mid;
	Console.WriteLine(l + " " + (u8)l + " " + (u8)Level.High + " " + (u8)Level.Top);
	Console.WriteLine((Level)11);
	Console.WriteLine(l < Level.High);

	switch (l)
	{
		Level.Low => Console.WriteLine("low");
		Level.Mid or Level.High => Console.WriteLine("mid or high");
		_ => Console.WriteLine("top");
	}
}
Terminal
larva run
Mid 2 10 11
Top
true
mid or high
  • A member without a value takes the previous member's value plus 1; the first one is 0.
  • A member's value is a constant: numbers, constants, earlier members of the same enum (Low or Level.Low), members of other enums, with | & ^ ~ + - * / % << >>. A member declared later cannot be used yet (AN1019).
  • An enum converts to and from its base only with a cast: (u8)l, (Level)11. Assigning an enum to an i32 without one is AN2084. A cast from a number no member has gives that number, which prints as the number.
  • ==, !=, <, <=, >, >= compare members by their values, and switch and patterns match members.
  • ToString(), and printing, give the member's name.

String enums

With a str base, each member is a string. A member without a value is its own name:

AntrilRun
import System;

public enum Word : str { Hello = "hola", World }

Main()
{
	Word w = Word.Hello;
	Console.WriteLine(w + " " + (str)w + " " + (str)Word.World);
	Console.WriteLine((Word)"hola");
}
Terminal
larva run
Hello hola World
Hello

(str)w gives the member's string; (Word)"hola" finds the member with that string.

Value enums

With a value base, each member stands for a value of that type, made with constant arguments:

Antril
public record value Rgb
{
	public:
		u8 R;
		u8 G;
		u8 B;
		new(u8 R, u8 G, u8 B);
}

public enum Color : Rgb
{
	Red = new Rgb(255, 0, 0),
	Green = new Rgb(0, 255, 0),
}

Rgb green = (Rgb)Color.Green;              // the member's value
Color red = (Color)new Rgb(255, 0, 0);     // Color.Red
  • Every member needs a value (AN1019).
  • Converting a value back to the enum needs equality by content: the base is a record value or declares op ==. A value no member has gives (Color)-1.
  • A bitflag enum cannot have a value base (AN1064).

Bitflag enums

In a bitflag enum, members without a value take the next free bit: 1, 2, 4, 8… The base is an integer type, u32 when none is given (AN1064 otherwise).

AntrilRun
import System;

public bitflag enum Access
{
	Read,
	Write,
	Execute,
	ReadWrite = Read | Write,
	All = declbits,
	Every = allbits,
}

Main()
{
	Access a = Access.Read | Access.Execute;
	Console.WriteLine(a);
	Console.WriteLine((u32)a + " " + (u32)Access.All + " " + (u32)Access.Every);
	Console.WriteLine(a.HasFlag(Access.Read) + " " + a.HasAny(Access.ReadWrite));
	Console.WriteLine(a.With(Access.Write) + ", " + a.Without(Access.Read) + ", " + a.Toggle(Access.Execute));
}
Terminal
larva run
Read | Execute
5 7 4294967295
true true
All, Execute, Read
  • |, &, ^ and ~ on members keep the enum's type.
  • declbits is every bit a declared member has; allbits is every bit of the base type. They exist only in bitflag enums.
  • Printing gives the name of the member with exactly that value (Read | Write | Execute is All above); any other combination joins the names of its bits with |.
  • HasFlag(x) (every bit of x is set), HasAny(x) (some bit of x is set), With(x), Without(x) and Toggle(x) come from an extension block of System.Core over every bitflag enum. A generic function can ask for a bitflag enum with the constraint T : bitflag enum.

Attributes on members

Members can carry attributes, which reflection reports as fields of the enum's type:

Antril
public enum Planet
{
	[Tag("closest")] Mercury,
	Venus,
}

See Attributes.

Was this page helpful?

No address is stored; your user name is, if you are signed in.

Esc