https://kotlinlang.org logo
Title
g

gildor

12/26/2018, 8:27 AM
One more thing which I use more often than 1:
val entity = hibernateRepository.getEntityById("some-id") ?: error("some-id is null")
1
c

Czar

12/26/2018, 8:28 AM
this is a different case. in my example entity is always not null, I'll edit it to make that obvious
g

gildor

12/26/2018, 8:30 AM
Anyway, I mean that to check for null I usually use elvis + error
c

Czar

12/26/2018, 8:36 AM
I see, that's too cumbersome for general use I think. E.g. here's an example from a code base I work on currently
class SomeEvent(
    val aEntityId: Long,
    val bEntityId: Long,
    val cEntityId: Long)

// all three are retrieved, something is done to them and event is being published:

publishEvent(
    SomeEvent(
        a.id ?: error(""),
        b.id ?: error(""),
        c.id ?: error("")
    )
)
text in error should be meaningful, so this is not something easy to read or write.
g

gildor

12/26/2018, 8:37 AM
I agree, it’s not something completely universal