Hello. Can any1 help me find out what is the issue...
# compose
m
Hello. Can any1 help me find out what is the issue in this code
Copy code
sealed class PrepareDevices {
    val canEditConfig: Boolean = this !is Connecting
    val canEditConfig2: Boolean = this != Connecting

    object Initial: PrepareDevices()
    object Connecting: PrepareDevices()
    object Connected: PrepareDevices()
}

class TestSingleton{

    @Test
    fun test(){
        val value = PrepareDevices.Initial
        Assert.assertTrue(value.canEditConfig)
    }

    @Test
    fun test2(){
        val value = PrepareDevices.Connecting
        Assert.assertTrue(value.canEditConfig.not())
    }

    @Test
    fun test3(){
        val value = PrepareDevices.Initial
        Assert.assertTrue(value.canEditConfig2)
    }

    @Test
    fun test4(){
        val value = PrepareDevices.Connecting
        Assert.assertTrue(value.canEditConfig2.not())
    }
}
Why does test4 fail. It doesnt make sense to me
v
Use
data object
👍 1
e
you're attempting to reference the
Connecting
instance before it has been initialized
not a
data
issue, nor Compose related
z
Those properties should be computed, no reason to allocate fields for them and that would probably fix this issue too since they’d be lazy.
e
^^ that, or as a constructor param
Copy code
sealed class PrepareDevices(
    val canEditConfig: Boolean,
) {
    object Initial: PrepareDevices(
        canEditConfig = true,
    )
    object Connecting: PrepareDevices(
        canEditConfig = false,
    )
    object Connected: PrepareDevices(
        canEditConfig = true,
    )
}
or as overrides
Copy code
sealed class PrepareDevices {
    abstract val canEditConfig: Boolean

    object Initial: PrepareDevices {
        override val canEditConfig: Boolean
            get() = true
    }
    object Connecting: PrepareDevices {
        override val canEditConfig: Boolean
            get() = false
    }
    object Connected: PrepareDevices {
        override val canEditConfig: Boolean
            get() = true
    }
}