Skip to content

Lang: val/var — are explicit mutability declarations needed? #113

@tmteam

Description

@tmteam

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

  1. Is val needed if TIC already prevents type violations?
  2. Is var needed if TIC already detects reassignment?
  3. Is val alone sufficient (everything reassignable by default)?
  4. Shadowing — is it val x = 42; val x = 'text'? Or separate syntax?
  5. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    NfunLanguageNFun-Lang full language modeSyntaxSyntactic and semantic improvements and sugarsWhat if?[Candidate]

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions