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
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.
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 …
Ha! Got an idea and it works:
fun <T> T.orNull() : T? {
val t: T? = this
return t
}
fun main(args: Array<String>) {
var str = “”.orNull()
str?.toUpperCase()
str.toUpperCase() // does not compile as desired
var num = 123.orNull()
var otherNum = num;
otherNum = otherNum?.inc()
otherNum = null
otherNum.inc() // 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?
This is a good solution indeed. Feel free to file an issue for standard library