val/var — candidate or necessity?
Currently NFun-Lang determines mutability automatically: if a variable is reassigned — mutable, otherwise — immutable.
Current behavior (no val/var)
x = 42 # immutable (one assignment)
y = 0
y = y + 1 # mutable (TIC detects reassignment)
Candidate: explicit val/var
val a = 42 # promise: won't be reassigned. Error if a = ...
var b = 0 # promise: will be reassigned
b += 1 # ok
a = 10 # COMPILE ERROR: val
Arguments FOR
- Typo protection:
val config = loadConfig() — accidental config = ... caught
- Documents intent: reader sees
val = won't change
- Shadowing:
val x = 42; val x = 'hello' — creates NEW variable (not reassignment)
- Familiar: Kotlin, Swift, Rust (let/let mut), JS (const/let)
Arguments AGAINST
- Compiler already knows: TIC infers mutability from usage
- Noise: in a scripting language
val/var are extra words
- Python precedent: Python manages without
val/var and it's not a problem
- Complexity: two more keywords, one more concept for beginners
Questions
- Is
val needed if TIC already prevents type violations?
- Is
var needed if TIC already detects reassignment?
- Is
val alone sufficient (everything reassignable by default)?
- Shadowing — is it
val x = 42; val x = 'text'? Or separate syntax?
- Scope:
val in a loop — new variable each iteration?
Decision
Deferred. Current implementation (implicit mutability) works. val/var can be added later without breaking changes (strict → lenient).
val/var — candidate or necessity?
Currently NFun-Lang determines mutability automatically: if a variable is reassigned — mutable, otherwise — immutable.
Current behavior (no val/var)
Candidate: explicit val/var
Arguments FOR
val config = loadConfig()— accidentalconfig = ...caughtval= won't changeval x = 42; val x = 'hello'— creates NEW variable (not reassignment)Arguments AGAINST
val/varare extra wordsval/varand it's not a problemQuestions
valneeded if TIC already prevents type violations?varneeded if TIC already detects reassignment?valalone sufficient (everything reassignable by default)?val x = 42; val x = 'text'? Or separate syntax?valin a loop — new variable each iteration?Decision
Deferred. Current implementation (implicit mutability) works.
val/varcan be added later without breaking changes (strict → lenient).