Suggestion: Static local variables

C has static local variables, e.g.:

int count() { 
    static int count = 0; 
    count++; 
    return count; 
} 

This feature leads to much more concise code, the equivalent in kotlin would be:

var count = 0
fun count(): Int {
    count++;
    return count;
}

In kotlin, this unecessarily polutes the classes namespace, leads to problems with variable name clashing, and can give the impression that the variable is applicable to the whole class, when in fact it is only applicable to the function.

Static local variables would easily allow definitions of variables that are only applicable to a certain function, but still need to preserve their state between multiple invocations.

The compilation for this would be easy, for JVM just create a private field at class level perhaps prepended with the function name.

Drawbacks:
It may be confusing for people transitioning from java to understand that the variable keeps its value between invocations.

2 Likes

You can easily simulate that with an object:

object Counter {
  private var counter = 0
  fun count(): Int = counter++
}

I don’t think adding a feature for this to be much worthwhile.

3 Likes

Source compatible:

object count {
    private var count = 0;
    operator fun invoke() = ++count
}
4 Likes

I personally find this to be far less concise. It breaks the rules of where field-declarations can be made and the current-way of doing it is far cleaner in my opinion.

1 Like

Kotlin tries to discourage people from writing non-pure top-level functions, functions that depend on some mutable state. Kotlin has classes and objects to encapsulate mutable state and to work with it. C-style programming with mutable global state is still supported in Kotiln for a variety of reasons (you are allowed to write a top-level var, which is essentially static). However, it is non-goal for Kotlin to make this C-style programming easier. The ability to declare static variables inside functions is missing in Kotlin by design, not by omission.

9 Likes