doc: another generics example (#6031)

pull/6035/head
Maciej Obarski 2020-08-01 15:34:23 +02:00 committed by GitHub
parent 0fb8074353
commit 2c6286b381
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 0 deletions

View File

@ -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