mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: another generics example (#6031)
This commit is contained in:
26
doc/docs.md
26
doc/docs.md
@ -1651,6 +1651,32 @@ user := users_repo.find_by_id(1)?
|
||||
post := posts_repo.find_by_id(1)?
|
||||
```
|
||||
|
||||
Another example:
|
||||
```v
|
||||
fn compare<T>(a, b T) int {
|
||||
if a < b {
|
||||
return -1
|
||||
}
|
||||
if a > b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
println(compare<int>(1,0)) // Outputs: 1
|
||||
println(compare<int>(1,1)) // 0
|
||||
println(compare<int>(1,2)) // -1
|
||||
|
||||
println(compare<string>('1','0')) // Outputs: 1
|
||||
println(compare<string>('1','1')) // 0
|
||||
println(compare<string>('1','2')) // -1
|
||||
|
||||
println(compare<float>(1.1, 1.0)) // Outputs: 1
|
||||
println(compare<float>(1.1, 1.1)) // 0
|
||||
println(compare<float>(1.1, 1.2)) // -1
|
||||
```
|
||||
|
||||
|
||||
## Concurrency
|
||||
|
||||
V's model of concurrency is very similar to Go's. To run `foo()` concurrently, just
|
||||
|
Reference in New Issue
Block a user