mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: move enum methods to enum section from sum types (#13843)
This commit is contained in:
parent
3a4bb09de2
commit
afdf8f50e7
88
doc/docs.md
88
doc/docs.md
@ -3095,6 +3095,50 @@ Output: `Grocery IDs: 0, 5, 6`.
|
||||
|
||||
Operations are not allowed on enum variables; they must be explicitly cast to `int`.
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
### Sum types
|
||||
|
||||
A sum type instance can hold a value of several different types. Use the `type`
|
||||
@ -3145,50 +3189,6 @@ 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`.
|
||||
|
Loading…
Reference in New Issue
Block a user