You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DOCS.md
+6-12Lines changed: 6 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1595,9 +1595,12 @@ _Can't be done without a series of method definitions for each data type. See th
1595
1595
1596
1596
### `final`
1597
1597
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:
1599
1599
```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: ...
1601
1604
```
1602
1605
1603
1606
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():
1621
1624
return x
1622
1625
```
1623
1626
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
-
1633
1627
#### Escape Hatch
1634
1628
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:
0 commit comments