doc: mention module shadowing (#7051)
parent
5b6eb7b2c9
commit
4f540e6ac3
28
doc/docs.md
28
doc/docs.md
|
@ -97,7 +97,7 @@ Anything you can do in other languages, you can do in V.
|
|||
</table>
|
||||
|
||||
<!--
|
||||
There are several special keywords, which you can put after the code fences for v.
|
||||
There are several special keywords, which you can put after the code fences for v.
|
||||
These are:
|
||||
compile - default, you do not need to specify it. cmd/tools/check-md.v compile the example.
|
||||
ignore - ignore the example, useful for examples that just use the syntax highlighting
|
||||
|
@ -297,11 +297,12 @@ In development mode the compiler will warn you that you haven't used the variabl
|
|||
In production mode (enabled by passing the `-prod` flag to v – `v -prod foo.v`)
|
||||
it will not compile at all (like in Go).
|
||||
|
||||
<!-- this should be `failcompile`, but it compiles -->
|
||||
```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 `<T>`, because the
|
||||
runtime parameter types. This is why `find_by_id` can omit `<T>`, 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.
|
||||
|
|
|
@ -24,14 +24,14 @@ The code is available <a href='https://github.com/vlang/v/tree/master/tutorials/
|
|||
|
||||
```
|
||||
wget https://github.com/vlang/v/releases/latest/download/linux.zip
|
||||
unzip linux.zip
|
||||
unzip v_linux.zip
|
||||
cd v
|
||||
sudo ./v symlink
|
||||
```
|
||||
|
||||
Now V should be globally available on your system.
|
||||
|
||||
> On macOS use `macos.zip`, on Windows - `windows.zip`.
|
||||
> On macOS use `v_macos.zip`, on Windows - `v_windows.zip`.
|
||||
If you use a BSD system, Solaris, Android, or simply want to install V
|
||||
from source, follow the simple instructions here:
|
||||
https://github.com/vlang/v#installing-v-from-source
|
||||
|
|
Loading…
Reference in New Issue