https://kotlinlang.org logo
#stdlib
Title
# stdlib
m

miha-x64

09/06/2019, 4:56 PM
reduce
would be great extension for
NonEmptyIterable
https://youtrack.jetbrains.com/issue/KT-21217
m

marstran

09/06/2019, 6:43 PM
Hmm, makes me think if you could combine contracts with some kind of phantom type feature. So you could do something like this:
Copy code
inline class NonEmptyList<T>(val list: List<T>) {
    contract { list.isNotEmpty() implies list is NonEmptyList }
}

fun someFunction(list: List<T>) {
    if (list.isNotEmpty()) {
        // list is "smart cast" (or conforms) to NonEmptyList.
    }
}
d

dr.dreigh

09/06/2019, 6:47 PM
m

marstran

09/06/2019, 6:51 PM
Not the contract and smart cast stuff 🙂
d

dr.dreigh

09/06/2019, 6:59 PM
I see yeah... be interested to see how that offers compile time safety from creating a non empty list, if you passed in a empty list at runtime (e.g from a web service response)
m

marstran

09/06/2019, 7:24 PM
The same compile time safety you would get by passing null to a non-null parameter at runtime. 😉
d

dr.dreigh

09/06/2019, 7:25 PM
oh, ok
will stick to the arrow one, might be better to implement non empty lists at the type level
2 Views