whats the best way to chain null checks. Say I ha...
# announcements
w
whats the best way to chain null checks. Say I had the following
Copy code
val configuration = if (config != null) config else
        {
            val retrieved = configRepo.findByMerchantId(id)
            if (retrieved != null) retrieved else MerchantBillConfig(id, emptyList(), CurrencyIso("XXX"), false)
        }
I mean if a value is passed use that else try to retrieve one else generate a default one.
w
Something similar to
Copy code
@Test
    fun asas1() {
        val asas: String? = "asas"
        val result: String = asas ?: getString()
        assertEquals("asas", result)
    }

    @Test
    fun asas2() {
        val asas: String? = null
        val result: String = asas ?: getString()
        assertEquals("someString", result)
    }

    private fun getString(): String {
        return "someString"
    }
d
Copy code
val configuration = config ?: configRepo.findByMerchantId(id) ?: MerchantBillConfig(id, emptyList(), CurrencyIso("XXX"), false)
w
Thanks
that makes more sense.