Is there any other idiomatic way to do this? ```r...
# announcements
j
Is there any other idiomatic way to do this?
Copy code
return when {
        pojo.dog != null -> {
            mapDog(this, pojo = pojo.dog!!)
        }
        <http://pojo.cat|pojo.cat> != null -> {
            mapCat(this, pojo = <http://pojo.cat|pojo.cat>!!)
        }
        else -> {
            mapDefault(this)
        }
    }
I have to add the
!!
because otherwise is saying
Smart cast is impossible because pojo.cat is an API class that is on a different module
d
Copy code
return
    pojo.dog?.let { mapDog(this,it) } ?:
    <http://pojo.cat?.let|pojo.cat?.let> { mapCat(this,it) } ?: 
    mapDefault(this)
👍 1
🤣 1
j
🤔
d
I don't know why @Milan Hruban laughed; that's a perfectly concise and idiomatic answer (in case that was a source of confusion, @Joan Colmenero).
m
a return when can indeed be perfectly translated to what Chris Hatton made. Kudos
m
@darkmoon_uk I laughed because I found that code very unreadable. Your code actually behaves differently than original snippet that Skizo posted. (in case that mapDog or mapCat return nullable values)
d
If they are nullable returns that is true yes.
Should have checked that - but assuming they are not, I think it reads ok.
j
What I wanted to avoid is to use the !! there
d
The compiler has already shared an opinion on that one 🙂
m
you should define two local vals just above your return called pojoDog and pojoCat getting their vals from the pojo. Then by using when on those, you shouldn't get the smartcast problem 🙂
val pojoDog = pojo.dog
j
Nice, let me try that
k
I'm on my phone, so I can't check, but could this work?
Copy code
pojo.dog?.let { return mapDog(this,it) }
    <http://pojo.cat?.let|pojo.cat?.let> { return mapCat(this,it) }
    return mapDefault(this)
1
👍 1