1
0
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:
Maciej Obarski
2020-08-01 15:34:23 +02:00
committed by GitHub
parent 0fb8074353
commit 2c6286b381

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