# Nullability and type inference

**URL:** https://discuss.kotlinlang.org/t/nullability-and-type-inference/436
**Category:** Uncategorized
**Created:** [October 16, 2014, 8:41am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436 "2014-10-16T08:41:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)
#### Post date: [October 16, 2014, 8:41am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/1 "2014-10-16T08:41:41Z")

</div>

This is something I wanted to ask for a long time. Now it struck my mind again. Type inference works well for non-null vars, e.g.

> val str1 = "foo"

But for optional nulls type inference seems not to work:

> val str2 **?** = "bar" // compiler error val str2 = "bar" **?** // compiler error

Think I tried out all positions where you can put the ?. Seems like I have to declare the type explicitly for things to compile if the var may be null:

> val str: String? = "baz" // compiles fine

While this is not really dramatic, it still feels a bit like a nuisance. Am I doing something wrong or could it be done in the compiler? Just curious ...

Regards, Oliver

P.S.: Thanks for M9. Kotlin is now in a state that you can really develop with it 🙂

---

<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: [October 16, 2014, 9:04am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/2 "2014-10-16T09:04:30Z")

</div>

I think you might have got the wrong impression about what "a?.b" is. It is not "call 'b' on 'a?' ", but rather "safe-call 'b' on 'a' ". I.e. '?.' is one operator name "safe call", not two, dot and questionmark. (Just for the record, it's the same for '?:').

> val str2 **?** = "bar" // compiler error val str2 = "bar" **?** // compiler error

So this sytax is not Kotlin :) You can't append question marks to expressions, nor variable declarations. I can think of a possible meaning for the first line, but can't see how to sensibly define semnatics for the secons, so that it is compatible with '?.' semantics.

---

<div class="post-metadata">

### Author: ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)
#### Post date: [October 16, 2014, 9:37am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/3 "2014-10-16T09:37:49Z")

</div>

Hello,

what I mean is this: Instead of “val str: String = “baz”” I can say “val str = “baz””. In the same way, in the case of “val str: String **?** = “baz”” I would like to leave the type information away as well (that str, which may also be null as in "val str: String **?** = “baz”, is a String). So this is why I came up with “val str **?** = “baz””. I’m aware of the elvis operator, but there is no method invocation being done here. It is about being able to say “val str: String **?** = “baz”” without having to declare the type, which here is String.

The point of the question was to ask whether there is a way of saying “val str: String **?** = “baz”” without having to declare a type String as in this example and let the compiler do the type inference work. I think this would be really nice. I don’t see why type inference should not be at the developers disposal when declaring a variable that may be null.

> So this sytax is not Kotlin 🙂 You can’t append question marks to expressions, nor variable declarations.

The point was to communicate that I had tried out all kinds of combinations to no avail. Maybe I’m just getting it wrong. I don’t know … 😉

---

<div class="post-metadata">

### Author: ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)
#### Post date: [October 16, 2014, 8:01pm UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/4 "2014-10-16T20:01:52Z")

</div>

Ha! Got an idea and it works:

> fun \<T\> T.orNull() : T? { &nbsp;&nbsp;val t: T? = this &nbsp;&nbsp;return t }
> 
> fun main(args: Array\<String\>) {  
> &nbsp;&nbsp;var str = “”.orNull()  
> &nbsp;&nbsp;str?.toUpperCase()  
> &nbsp;&nbsp;str.toUpperCase() // does not compile as desired
> 
> &nbsp;&nbsp;var num = 123.orNull()  
> &nbsp;&nbsp;var otherNum = num;  
> &nbsp;&nbsp;otherNum = otherNum?.inc()  
> &nbsp;&nbsp;otherNum = null  
> &nbsp;&nbsp;otherNum.inc()&nbsp;&nbsp;// does not compile as desired  
> }

I used val in my first post instead of var. Think that’s why it could not be understood. Mea culpa. Anyway extension methods in Kotlin are really amazing :-). Well, I still prefer \<var str? = “”\>. But who cares. Maybe something like orNull() should be in the base library?

---

<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: [October 16, 2014, 10:30pm UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/5 "2014-10-16T22:30:14Z")

</div>

This is a good solution indeed. Feel free to file an issue for standard library

---

<div class="post-metadata">

### Author: ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)
#### Post date: [October 17, 2014, 9:12am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/6 "2014-10-17T09:12:38Z")

</div>

Okay, here it is:

[KT-6052](https://youtrack.jetbrains.com/issue/KT-6052)

---

<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: [October 17, 2014, 9:49am UTC](https://discuss.kotlinlang.org/t/nullability-and-type-inference/436/7 "2014-10-17T09:49:29Z")

</div>

Thanks
