From 4f540e6ac353335844856f676bda358099234bdd Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Tue, 1 Dec 2020 16:09:33 +0100 Subject: [PATCH] doc: mention module shadowing (#7051) --- doc/docs.md | 28 +++++++++++++------ .../building-a-simple-web-blog-with-vweb.md | 4 +-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 21c1f6e3d5..5b70ecc890 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -97,7 +97,7 @@ Anything you can do in other languages, you can do in V. ```v fn main() { a := 10 if true { - a := 20 // error: shadowed variable + a := 20 // error: redefinition of `a` } // warning: unused variable `a` } @@ -310,6 +311,17 @@ fn main() { Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name that is already used in a parent scope will cause a compilation error. +You can shadow imported modules though, as it is very useful in some situations: +```v ignore +import ui +import gg + +fn draw(ctx &gg.Context) { + gg := ctx.parent.get_ui().gg + gg.draw_rect(...) +} +``` + ## Types ### Primitive types @@ -1012,7 +1024,7 @@ Here `i` doesn't need to be declared with `mut` since it's always going to be mu #### Labelled break & continue `break` and `continue` control the innermost `for` loop by default. -You can also use `break` and `continue` followed by a label name to refer to an outer `for` +You can also use `break` and `continue` followed by a label name to refer to an outer `for` loop: ```v @@ -1249,7 +1261,7 @@ struct ButtonConfig { height int = 20 } -struct Button { +struct Button { text string width int height int @@ -1738,7 +1750,7 @@ sum := World(Moon{}) assert sum.type_name() == 'Moon' println(sum) ``` -The built-in method `type_name` returns the name of the currently held +The built-in method `type_name` returns the name of the currently held type. #### Dynamic casts @@ -1781,7 +1793,7 @@ if w is Mars { } } ``` -`w` has type `Mars` inside the body of the `if` statement. This is +`w` has type `Mars` inside the body of the `if` statement. This is known as *flow-sensitive typing*. You can also specify a variable name: ```v ignore @@ -2025,7 +2037,7 @@ At the moment only one type parameter named `T` is supported. Currently generic function definitions must declare their type parameters, but in future V will infer generic type parameters from single-letter type names in -runtime parameter types. This is why `find_by_id` can omit ``, because the +runtime parameter types. This is why `find_by_id` can omit ``, because the receiver argument `r` uses a generic type `T`. Another example: @@ -2187,7 +2199,7 @@ fn main () { // do something if no channel has become ready within 0.5s } } -} +} ``` The timeout branch is optional. If it is absent `select` waits for an unlimited amount of time. diff --git a/tutorials/building-a-simple-web-blog-with-vweb.md b/tutorials/building-a-simple-web-blog-with-vweb.md index 41a87604c7..4f36bd4404 100644 --- a/tutorials/building-a-simple-web-blog-with-vweb.md +++ b/tutorials/building-a-simple-web-blog-with-vweb.md @@ -24,14 +24,14 @@ The code is available