From 2c6286b3814c8cfcf65a678bb21798a9520c2f7f Mon Sep 17 00:00:00 2001 From: Maciej Obarski Date: Sat, 1 Aug 2020 15:34:23 +0200 Subject: [PATCH] doc: another generics example (#6031) --- doc/docs.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index c69c52e4ba..6e3b978345 100644 --- a/doc/docs.md +++ b/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(a, b T) int { + if a < b { + return -1 + } + if a > b { + return 1 + } + return 0 +} + +println(compare(1,0)) // Outputs: 1 +println(compare(1,1)) // 0 +println(compare(1,2)) // -1 + +println(compare('1','0')) // Outputs: 1 +println(compare('1','1')) // 0 +println(compare('1','2')) // -1 + +println(compare(1.1, 1.0)) // Outputs: 1 +println(compare(1.1, 1.1)) // 0 +println(compare(1.1, 1.2)) // -1 +``` + + ## Concurrency V's model of concurrency is very similar to Go's. To run `foo()` concurrently, just