From 3907a1ab497323a9fa12af12eadbb07a326e5217 Mon Sep 17 00:00:00 2001 From: Ben-Fields <33070053+Ben-Fields@users.noreply.github.com> Date: Sun, 21 Feb 2021 03:54:13 -0600 Subject: [PATCH] docs: document enum <-> int assignment and casting (#8872) --- doc/docs.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index b35669f58d..5ed38f0d91 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -2132,6 +2132,25 @@ color := Color.@none println(color) ``` +Integers may be assigned to enum fields. + +```v +enum Grocery { + apple + orange = 5 + pear +} + +g1 := int(Grocery.apple) +g2 := int(Grocery.orange) +g3 := int(Grocery.pear) +println('Grocery IDs: $g1, $g2, $g3') +``` + +Output: `Grocery IDs: 0, 5, 6`. + +Operations are not allowed on enum variables; they must be explicity cast to `int`. + ### Sum types A sum type instance can hold a value of several different types. Use the `type`