Last active
May 15, 2020 04:38
-
-
Save amatkivskiy/b1222bcade61b9aed788d2bbcdde6a70 to your computer and use it in GitHub Desktop.
For medium article: https://medium.com/@amatkivskiy/legacy-code-pitfalls-35feda5acb71
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arrange | |
data class Parent(val child: Nested?) | |
class Nested(val child: Nested?, val isValid : Boolean = false) | |
// Assume you got this object from the dark and deep internals of your app | |
val parentObj = getParentObjFromInternals() | |
// This is straightforward but it won't work | |
if (parentObj.child?.child?.child?.isValid) { | |
// Error. | |
// Type mismatch. Required: Boolean, Found: Boolean? | |
} | |
// So you need to workaround this | |
if (parentObj.child?.child?.child?.isValid == true) { | |
// Do something | |
} | |
fun getParentObjFromInternals() = Parent(Nested(Nested(Nested(null, true)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment