Anyone know why type checking hates the `val foo: ...
# announcements
s
Anyone know why type checking hates the
val foo: String? by defaultProperty { null }
? It only goes away by explicitly defining the type of
defaultProperty<String?>
l
whoa syntax highlighting in slack? how do you do that?
s
Unrelated but when you insert the snippet you can select the language
d
null
is inferred to be of type
Nothing?
here, so
defaultProperty { null }
is a
ReadWriteProperty<Any?, Nothing?>
. If you only want a
val
you should use
ReadOnlyProperty
, which has its type parameter marked
out
, in that scenario your code should compile.
s
@diesieben07 This needs to be both read/write, I have some vars that are mutable. Can I fix
ReadWriteProperty
’s generics?
d
In that case you'll have to specify it explicitly. However in the 1.4-M1 blogpost there was a mention about the new type inference improving this situation
If it needs to be a
ReadWriteProperty
then you have to specify the type, otherwise there is no way for the compiler to know that you want a
String?
there.
s
Seems strange that the type is inferred from the block instead of the
reified T
d
You write
defaultProperty { null }
, without specifying the
T
explicitly. So the compiler has to infer it from the arguments to
defaultProperty
, which is just
{ null }
, which is of type
() -> Nothing?
-
T
is therefor inferred to be
Nothing?
s
Right, I meant that you would think the type would be inferred from the
val foo: T
d
Thats not how type inference works at the moment though, it seems.
But maybe the improvements in 1.4 will fix your use-case
s
I’m compiling with
-Xnew-inference
although it doesn’t seem to help.
d
The improvements are part of 1.4-m1, i think. check out the blogpost
1
s
Will do, thanks!