From 625dc6e0ff66630612d2f7a6cc25044c1cc27a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= <33205215+remimimimi@users.noreply.github.com> Date: Mon, 28 Jun 2021 16:25:02 +0300 Subject: [PATCH] doc: enum methods (#10572) --- doc/docs.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index cec4a8a45c..631f3fe518 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -2675,6 +2675,50 @@ fn main() { } ``` +Enums can have methods, just like structs + +```v +enum Cycle { + one + two + three +} + +fn (c Cycle) next() Cycle { + match c { + .one { + return .two + } + .two { + return .three + } + .three { + return .one + } + } +} + +mut c := Cycle.one +for _ in 0 .. 10 { + println(c) + c = c.next() +} +``` + +Output: +``` +one +two +three +one +two +three +one +two +three +one +``` + #### Dynamic casts To check whether a sum type instance holds a certain type, use `sum is Type`.