How do you usually deal with `Smart cast to 'X' is...
# random
r
How do you usually deal with
Smart cast to 'X' is impossible, because 'x' is a property that has open or custom getter
? I usually do
val x = x
locally but then I get a name shadowing warning. I don't really like having to find another name just because I have a custom getter. What would be a clean solution?
1
s
.let
can be helpful, and (I think) won’t produce the warning:
Copy code
x?.let { x -> ... }
r
Yes, but it's less readable I think. Example:
Copy code
city?.let { city ->
                Text(city.name)
                Text(city.country)
            } ?: run {
                Text("…")
            }
Copy code
val city = city // I'd rather not do that
            if (city == null) {
                Text("…")
            } else {
                Text(city.name)
                Text(city.country)
            }
h
I find the first of your two examples to be more readable than the second, actually, since I will not spend time on wondering about the
val x = x
line.
4
s
You could always extract a method with
x
as a parameter
☝️ 1