From e9641875c35843a87e1bf535ffe3ba4df317eeba Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 17 Feb 2025 18:54:19 +0200 Subject: [PATCH] doc: describe what enums are in docs.md (#23750) --- doc/docs.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 8bd30ba41b..5dc16750a0 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3466,11 +3466,14 @@ This is a special case of a [sum type](#sum-types) declaration. ### Enums +An enum is a group of constant integer values, each having its own name, +whose values start at 0 and increase by 1 for each name listed. +For example: ```v enum Color as u8 { - red - green - blue + red // the default start value is 0 + green // the value is automatically incremented to 1 + blue // the final value is now 2 } mut color := Color.red @@ -3482,6 +3485,7 @@ match color { .green { println('the color was green') } .blue { println('the color was blue') } } +println(int(color)) // prints 1 ``` The enum type can be any integer type, but can be omitted, if it is `int`: `enum Color {`.