doc: simplify interface method definition example (#8468)
							parent
							
								
									96466e2b15
								
							
						
					
					
						commit
						3be5795724
					
				
							
								
								
									
										34
									
								
								doc/docs.md
								
								
								
								
							
							
						
						
									
										34
									
								
								doc/docs.md
								
								
								
								
							| 
						 | 
					@ -1906,20 +1906,25 @@ interface Speaker {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dog := Dog{'Leonberger'}
 | 
					dog := Dog{'Leonberger'}
 | 
				
			||||||
cat := Cat{'Siamese'}
 | 
					cat := Cat{'Siamese'}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
mut arr := []Speaker{}
 | 
					mut arr := []Speaker{}
 | 
				
			||||||
arr << dog
 | 
					arr << dog
 | 
				
			||||||
arr << cat
 | 
					arr << cat
 | 
				
			||||||
for item in arr {
 | 
					for item in arr {
 | 
				
			||||||
	println('a $item.breed ${typeof(item).name} says: $item.speak()')
 | 
						println('a $item.breed says: $item.speak()')
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
A type implements an interface by implementing its methods and fields.
 | 
					A type implements an interface by implementing its methods and fields.
 | 
				
			||||||
There is no explicit declaration of intent, no "implements" keyword.
 | 
					There is no explicit declaration of intent, no "implements" keyword.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Casting an interface
 | 
				
			||||||
 | 
					
 | 
				
			||||||
We can test the underlying type of an interface using dynamic cast operators:
 | 
					We can test the underlying type of an interface using dynamic cast operators:
 | 
				
			||||||
```v oksyntax
 | 
					```v oksyntax
 | 
				
			||||||
fn announce(s Speaker) {
 | 
					interface Something {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn announce(s Something) {
 | 
				
			||||||
	if s is Dog {
 | 
						if s is Dog {
 | 
				
			||||||
		println('a $s.breed dog') // `s` is automatically cast to `Dog` (smart cast)
 | 
							println('a $s.breed dog') // `s` is automatically cast to `Dog` (smart cast)
 | 
				
			||||||
	} else if s is Cat {
 | 
						} else if s is Cat {
 | 
				
			||||||
| 
						 | 
					@ -1931,6 +1936,8 @@ fn announce(s Speaker) {
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
For more information, see [Dynamic casts](#dynamic-casts).
 | 
					For more information, see [Dynamic casts](#dynamic-casts).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Interface method definitions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Also unlike Go, an interface may implement a method.
 | 
					Also unlike Go, an interface may implement a method.
 | 
				
			||||||
These methods are not implemented by structs which implement that interface.
 | 
					These methods are not implemented by structs which implement that interface.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1941,33 +1948,28 @@ implemented on the interface is called.
 | 
				
			||||||
```v
 | 
					```v
 | 
				
			||||||
struct Cat {}
 | 
					struct Cat {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface Adoptable {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn (c Cat) speak() string {
 | 
					fn (c Cat) speak() string {
 | 
				
			||||||
	return 'meow!'
 | 
						return 'meow!'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					interface Adoptable {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn (a Adoptable) speak() string {
 | 
					fn (a Adoptable) speak() string {
 | 
				
			||||||
	return 'adopt me!'
 | 
						return 'adopt me!'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn (a Adoptable) adopt() ?&Cat {
 | 
					 | 
				
			||||||
	if a is Cat {
 | 
					 | 
				
			||||||
		return a
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		return error('This cannot be adopted.')
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn new_adoptable() Adoptable {
 | 
					fn new_adoptable() Adoptable {
 | 
				
			||||||
	return Cat{}
 | 
						return Cat{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
	adoptable := new_adoptable()
 | 
						cat := Cat{}
 | 
				
			||||||
	println(adoptable.speak()) // adopt me!
 | 
						assert cat.speak() == 'meow!'
 | 
				
			||||||
	cat := adoptable.adopt() or { return }
 | 
						a := new_adoptable()
 | 
				
			||||||
	println(cat.speak()) // meow!
 | 
						assert a.speak() == 'adopt me!'
 | 
				
			||||||
 | 
						if a is Cat {
 | 
				
			||||||
 | 
							println(a.speak()) // meow!
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue