Skip to content

Commit 947f825

Browse files
committed
Update final docs
1 parent 13f26e8 commit 947f825

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

DOCS.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,12 @@ _Can't be done without a series of method definitions for each data type. See th
15951595

15961596
### `final`
15971597

1598-
Coconut supports `final` declarations to mark variables as constant, preventing reassignment within the same scope. The syntax is:
1598+
Coconut supports `final` declarations to mark variables as constant, preventing reassignment within the same scope. `final` is supported anywhere a variable can be declared, such as:
15991599
```coconut
1600-
final <name> = <value>
1600+
final x = 1
1601+
final x: int = 1
1602+
def f(final x): ...
1603+
for final i in items: ...
16011604
```
16021605

16031606
Once a variable is declared `final`, it cannot be reassigned within that scope. Attempting to reassign a final variable will result in a compile-time error.
@@ -1621,18 +1624,9 @@ def f():
16211624
return x
16221625
```
16231626

1624-
#### Usage Contexts
1625-
1626-
The `final` keyword can be used in:
1627-
- Variable assignments: `final x = 1`
1628-
- Typed assignments: `final x: int = 1`
1629-
- Function parameters: `def f(final x): ...`
1630-
- For loops: `for final i in items: ...`
1631-
- Match patterns: `case final x: ...`
1632-
16331627
#### Escape Hatch
16341628

1635-
To bypass final checking (e.g., for metaprogramming), prefix the variable name with a backslash:
1629+
To bypass final checking, prefix the variable name with a backslash:
16361630
```coconut
16371631
final x = 1
16381632
\x = 2 # OK - bypasses final check

0 commit comments

Comments
 (0)