docs: improve the interface examples (#13422)
parent
9ed18efa53
commit
b205e2fc67
24
doc/docs.md
24
doc/docs.md
|
@ -2647,20 +2647,20 @@ particularly useful for initializing a C library.
|
|||
## Type Declarations
|
||||
|
||||
### Interfaces
|
||||
|
||||
```v
|
||||
// interface-example.1
|
||||
struct Dog {
|
||||
breed string
|
||||
}
|
||||
|
||||
struct Cat {
|
||||
breed string
|
||||
}
|
||||
|
||||
fn (d Dog) speak() string {
|
||||
return 'woof'
|
||||
}
|
||||
|
||||
struct Cat {
|
||||
breed string
|
||||
}
|
||||
|
||||
fn (c Cat) speak() string {
|
||||
return 'meow'
|
||||
}
|
||||
|
@ -2671,6 +2671,7 @@ interface Speaker {
|
|||
speak() string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dog := Dog{'Leonberger'}
|
||||
cat := Cat{'Siamese'}
|
||||
|
||||
|
@ -2680,6 +2681,7 @@ arr << cat
|
|||
for item in arr {
|
||||
println('a $item.breed says: $item.speak()')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implement an interface
|
||||
|
@ -2691,6 +2693,7 @@ An interface can have a `mut:` section. Implementing types will need
|
|||
to have a `mut` receiver, for methods declared in the `mut:` section
|
||||
of an interface.
|
||||
```v
|
||||
// interface-example.2
|
||||
module main
|
||||
|
||||
pub interface Foo {
|
||||
|
@ -2734,20 +2737,29 @@ fn fn1(s Foo) {
|
|||
|
||||
We can test the underlying type of an interface using dynamic cast operators:
|
||||
```v oksyntax
|
||||
// interface-exmaple.3 (continued from interface-exampe.1)
|
||||
interface Something {}
|
||||
|
||||
fn announce(s Something) {
|
||||
if s is Dog {
|
||||
println('a $s.breed dog') // `s` is automatically cast to `Dog` (smart cast)
|
||||
} else if s is Cat {
|
||||
println('a $s.breed cat')
|
||||
println('a cat speaks $s.speak()')
|
||||
} else {
|
||||
println('something else')
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dog := Dog{'Leonberger'}
|
||||
cat := Cat{'Siamese'}
|
||||
announce(dog)
|
||||
announce(cat)
|
||||
}
|
||||
```
|
||||
|
||||
```v
|
||||
// interface-example.4
|
||||
interface IFoo {
|
||||
foo()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue