Marc Knaup
03/05/2020, 2:48 AM!== null and === null instead of != null and == null due to not using equals?Svyatoslav Kuzmich [JB]
03/05/2020, 3:08 AMNote that there’s no point in optimizing your code when comparing tohttps://kotlinlang.org/docs/reference/equality.html#structural-equalityexplicitly:nullwill be automatically translated toa == nulla === null
Marc Knaup
03/05/2020, 3:10 AM=== null and !== null then 🙂Marc Knaup
03/05/2020, 3:24 AMSvyatoslav Kuzmich [JB]
03/05/2020, 3:35 AMdynamic type, equality operators would be translated to corresponding JS a == null or a === null . First one would be true if a is null or undefined . Second would strictly check for null.Svyatoslav Kuzmich [JB]
03/05/2020, 3:46 AMMarc Knaup
03/05/2020, 3:51 AMundefined?Marc Knaup
03/05/2020, 3:55 AMundefined suddenly turn into null or will equality in common code suddenly change meaning?Svyatoslav Kuzmich [JB]
03/05/2020, 3:57 AMundefined and null JS values are treated as null in Kotlin (Apparently except in ===). Kotlin null is represented as JS null , but undefined could come from interop.Marc Knaup
03/05/2020, 3:58 AMnull value that isn’t identical to null.Svyatoslav Kuzmich [JB]
03/05/2020, 3:58 AM=== would change the meaning in common code. Everything else should work fine.Marc Knaup
03/05/2020, 4:02 AMfun main() {
var x: String? = undefined
if (x !== null)
println(x.toUpperCase())
}
Unhandled JavaScript exception: Cannot read property 'toUpperCase' of undefinedSvyatoslav Kuzmich [JB]
03/05/2020, 4:13 AM=== we would need to put null-checks on every === with nullable types on both sides. But people expect === to be very fast.Marc Knaup
03/05/2020, 4:18 AM=== null as == null would only have to be done in JS code, wouldn’t it? Is there a performance difference in that case?
From a Kotlin perspective it would probably be much more reasonable, consistent and predictable if undefined is always treated as null in equality and identity checks.
Developers can still check === undefined.
No normalization needed then.Svyatoslav Kuzmich [JB]
03/05/2020, 4:29 AM=== null as == null is relatively cheap indeed. But treating a === b as a == b , where a and b are nullable, would break identity semantics due to coercion rules in JS. You would have to translate a === b to something like a == null ? b == null : a === b which could be a performance problem.Marc Knaup
03/05/2020, 4:35 AMnull.Marc Knaup
03/05/2020, 4:37 AMjsIsIdentical(a, b) or similar.Svyatoslav Kuzmich [JB]
03/05/2020, 4:38 AM=== null would at least solve the smart cast problem you mentioned.