| ... | @@ -2377,7 +2377,36 @@ test "labeled break from labeled block expression" { | ... | @@ -2377,7 +2377,36 @@ test "labeled break from labeled block expression" { |
| 2377 | {#code_end#} | 2377 | {#code_end#} |
| 2378 | <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p> | 2378 | <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p> |
| 2379 | {#see_also|Labeled while|Labeled for#} | 2379 | {#see_also|Labeled while|Labeled for#} |
| | 2380 | |
| | 2381 | {#header_open|Shadowing#} |
| | 2382 | <p>It is never allowed for an identifier to "hide" another one by using the same name:</p> |
| | 2383 | {#code_begin|test_err|redefinition#} |
| | 2384 | const pi = 3.14; |
| | 2385 | |
| | 2386 | test "inside test block" { |
| | 2387 | // Let's even go inside another block |
| | 2388 | { |
| | 2389 | var pi: i32 = 1234; |
| | 2390 | } |
| | 2391 | } |
| | 2392 | {#code_end#} |
| | 2393 | <p> |
| | 2394 | Because of this, when you read Zig code you can rely on an identifier always meaning the same thing, |
| | 2395 | within the scope it is defined. Note that you can, however use the same name if the scopes are separate: |
| | 2396 | </p> |
| | 2397 | {#code_begin|test#} |
| | 2398 | test "separate scopes" { |
| | 2399 | { |
| | 2400 | const pi = 3.14; |
| | 2401 | } |
| | 2402 | { |
| | 2403 | var pi: bool = true; |
| | 2404 | } |
| | 2405 | } |
| | 2406 | {#code_end#} |
| 2380 | {#header_close#} | 2407 | {#header_close#} |
| | 2408 | {#header_close#} |
| | 2409 | |
| 2381 | {#header_open|switch#} | 2410 | {#header_open|switch#} |
| 2382 | {#code_begin|test|switch#} | 2411 | {#code_begin|test|switch#} |
| 2383 | const assert = @import("std").debug.assert; | 2412 | const assert = @import("std").debug.assert; |