# I don't want to specify initializer for local, nullable or primitive variable

**URL:** <https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966>\
**Category:** Uncategorized\
**Created:** [December 19, 2012, 2:25pm UTC](https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966 "2012-12-19T14:25:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![develar](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@develar](https://discuss.kotlinlang.org/u/develar)\
**Post date:** [December 19, 2012, 2:25pm UTC](https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966/1 "2012-12-19T14:25:41Z")

</div>

Latest koltin enforce me to specify initializer for local variable.

before:  
{code}

1. val doneAsync:Boolean
2. try {
3. doneAsync = task(pageManager.getDom(tabId), it)
4. }
5. finally {
6. if (!doneAsync) {
7. it()
8. }
9. }{code}

after:  
{code}

1. var doneAsync = false
2. try {
3. doneAsync = task(pageManager.getDom(tabId), it)
4. }
5. finally {
6. if (!doneAsync) {
7. it()
8. }
9. }{code}

OMG. Fucking stupid Java. I want to specify doneAsync as immutable value, not as variable. And now (after yeasterday commits) I cannot.

before:  
{code}

1. var candidates:MutableList\<Tab\>?
2. var newTab:Tab?
3. var existingTab:Tab?
4. for (tab in it) {…{code}

after:  
{code}

1. var candidates:MutableList\<Tab\>? = null
2. var newTab:Tab? = null
3. var existingTab:Tab? = null

{code}  
WTF. Why “?” (specify nullable) is not enough?

Are you sure that we need these changes?

---

<div class="post-metadata">

**Author:** ![svtk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svtk/32/1499_2.png) [@svtk](https://discuss.kotlinlang.org/u/svtk)\
**Post date:** [December 19, 2012, 4:07pm UTC](https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966/2 "2012-12-19T16:07:16Z")

</div>

Apparently you want [http://youtrack.jetbrains.com/issue/KT-2957](http://youtrack.jetbrains.com/issue/KT-2957). Please vote =)

Not nullable types (except corresponding to primitives) obviously can’t have default values.

Primitives theoretically can, but it seems to me better to write values explicitly:

```kotlin
var sum: Int
for (el in list) {
  sum += el
}
```

Here

```kotlin
var sum = 0
```

will be much better!  
The same with booleans.

As for the first example, ‘doneAsync’ is semantically mutable variable (like ‘v’ here):

```kotlin
    val v: Boolean?
  try {
  doSmth(v)  // If you’re able to use v with default ‘null’ value here
  v = true   // and reassign v here,
  doSmth(v)  // then ‘v’ behaves as a mutable variable, so it should be a var.
  }
  finally {
  if (!v) {  
  }
  }

```

---

<div class="post-metadata">

**Author:** ![develar](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@develar](https://discuss.kotlinlang.org/u/develar)\
**Post date:** [December 19, 2012, 4:48pm UTC](https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966/3 "2012-12-19T16:48:11Z")

</div>

first example for me equals to:

``

```kotlin

val doneAsync = task(pageManager.getDom(tabId), it)
if (!doneAsync) {
  it()
}

```

But I need to use try-finally. In Java I cannot use “final” modifier in this case (it is sad :)) and now I cannot do it in Kotlin. May be it is semantically correct, but really doneAsync is immutable (i.e. I must not modify it’s value). I don’t want change val to var only due to try-finally.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [December 24, 2012, 9:52am UTC](https://discuss.kotlinlang.org/t/i-dont-want-to-specify-initializer-for-local-nullable-or-primitive-variable/966/4 "2012-12-24T09:52:57Z")

</div>

The problem is not modification, but initialization. You never modify doneAsync, but then you may never initialize it when you reach 'finally'.
