mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: enum methods (#10572)
This commit is contained in:
parent
806719786f
commit
625dc6e0ff
44
doc/docs.md
44
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
|
#### Dynamic casts
|
||||||
|
|
||||||
To check whether a sum type instance holds a certain type, use `sum is Type`.
|
To check whether a sum type instance holds a certain type, use `sum is Type`.
|
||||||
|
Loading…
Reference in New Issue
Block a user