Hadi Lashkari
05/04/2020, 10:01 AMPair(a,b) as? Pair<String, Boolean>
where a
or b
can be null, there is a Java warning that says Unchecked cast: Pair<String?, Boolean?> to Pair<String, Boolean>
. By supporting more strong typing system which supports generics type checking, we can make sure we're developing less buggy programs. I want to know is it possible to do so? For instance, why we cannot hold type info in the KClass in the runtime to check these kind of stuff? Thank you.gildor
05/04/2020, 11:11 AMfun <A : Any, B : Any> Pair<A?, B?>.takeIfNonNull(): Pair<A, B>? {
val first = first ?: return null
val second = second ?: return null
return Pair(first, second)
// Also can be optimized with unsave cast:
// @Suppress("UNREACHABLE_CODE")
// return this as Pair<A, B>
}
gildor
05/04/2020, 11:15 AMHadi Lashkari
05/04/2020, 11:41 AMas?
operator. Search in your code base and give me some of your use-cases, then I would make those types generic and that would be my use-case.Hadi Lashkari
05/04/2020, 11:42 AMgildor
05/04/2020, 12:05 PMgildor
05/04/2020, 12:06 PMHadi Lashkari
05/04/2020, 12:10 PMHadi Lashkari
05/04/2020, 12:10 PMis
? give me your use-cases so I can make them generic.Shawn
05/04/2020, 3:54 PMHadi Lashkari
05/04/2020, 6:08 PMval a:String? = ...
val b:Boolean? = ...
(Pair(a, b) as? Pair<String, Boolean>)?.let{(aa,bb) ->
doSomething(aa, bb)
}
which could be a solution for https://discuss.kotlinlang.org/t/kotlin-null-check-for-multiple-nullable-vars/1946/55 discussion. But I can think about many more use-cases.StavFX
05/04/2020, 8:26 PMif (a != null && b !=null)
gildor
05/05/2020, 1:25 AMHadi Lashkari
05/05/2020, 8:01 AMHadi Lashkari
05/05/2020, 8:05 AMgildor
05/05/2020, 10:23 AMBut it’s shorter and saferIt’s not safer, because you repeat type declaration, so it’s a lot easier to introduce bug: This works
val a:String? = ...
val b:Boolean? = ...
(Pair(a, b) as? Pair<String, Boolean>)?.let{(aa,bb) ->
doSomething(aa, bb)
}
This doesn’t work, it’s completely broken because of very subtle bug
val a:String? = ...
val b:Boolean? = ...
(Pair(b, a) as? Pair<String, Boolean>)?.let{(aa,bb) ->
doSomething(aa, bb)
}
Also it’s not shorter:
val a:String? = ...
val b:Boolean? = ...
if (a != null && b != null) {
doSomething(a, b)
}
49 characters for check and call instead of 76 in your example
Also because Kotlin does smart cast in this case, it would be checked on compile time for nullability. If doSomething requires non-null arguments, it’s impossible for example to do such mistake and get NPE as in Java
if (a != null || b != null) {
doSomething(a, b)
}
You see, ||
instead of &&
, Smart cast will not work, compilation will fail, in java and other languages without nullability support in type system it will fail on runtimegildor
05/05/2020, 10:26 AMfact everything that Java and Kotlin can do someone can code in bytecodeIt’s not true, because a lot of language features of Java and (even more) Kotlin are done on compile time, so a lot of information is not available on runtime, only compile time, such as big part of type system, not only generics
Hadi Lashkari
05/05/2020, 11:47 AM49 characters for check and call instead of 76 in your exampleYou need to repeat those 49 characters all around when you pass
Pair(a, b)
around methods to use a
and b
but you need 30 of them in those method to check and use a
and b
again and again!
This doesn’t work, it’s completely brokenGood to see you're seeing its advantage and safety 🙂
it’s impossible for example to do such mistake and get NPE as in JavaI'm trying to think about Kotlin as an independent language of Java because it's support multiplatform, but you keep mentioning Java, and it's not your fault! Maybe some decisions about Kotlin was contradictory! For instance, they decided to go for multiplatform and be independent but they keep being dependent to JVM environment! It's confusing!
so a lot of information is not available on runtime, only compile time, such as big part of type systemyou mentioned my exact point of view here! "type system"! so stronger type system is what these languages do on above of bytecode. I need it to be more stronger, that's just it. Thank you for helping me clearing my mind.
gildor
05/05/2020, 12:34 PMgildor
05/05/2020, 12:37 PMGood to see you're seeing its advantage and safety 🙂No, I don't see, if this syntax would work on runtime, it will be still broken, see, there is a bug and usage of cast in this case is error prone, check my example carefully
Hadi Lashkari
05/05/2020, 1:11 PMa
and b
isn't it? So the doSomething
would not get called or if this pair had came from arguments of the method it would not get compiled. Is that what you've wanted to mention? Because I think this is what we're looking at by typing system.gildor
05/05/2020, 1:38 PM