<@U5UU34LPK> I often resort to `!!` on `.min()` or...
# language-proposals
p
@karelpeeters I often resort to
!!
on
.min()
or
.max()
calls where I know the collection is not empty. I kind of wish we had a
NonEmptyList
that would return non-nullable types from such functions. Do you guys have a better way to handle this?
b
A non-empty list type would be nice. Otherwise one cannot be sure that another thread has not emptied the collection by the time you call
.min()
d
They could always add support for dependent types 🧌 However I guess you could add extension methods for min/max that either accepted a fallback value if the list was empty, or returned null if the list was empty (then use
?:
for your fallback value). Or you could stick with
!!
you’ll get some kind of exception if you ever do have an empty list, which I think is fine because you’re expecting something not empty?
p
min()
,
max()
already return
null
for empty lists AFAIK
so you can do:
Copy code
myList.min() ?: 0
// or
myList.min()!!
but both seem ugly when I know 100% that the list is not empty, maybe because it's in a local variable that I have just initialized with values one row above.
b
I personally dislike how the stdlib diverges from its common approach of
.fooOrNull()
here, but also personally, I wish "or null" was the default (like these) instead of the current "or crash"
p
whatever the choice is I agree that it should at least be consistent
☝🏼 2
1
g
Copy code
fun List<Int>.minOrZero() = min ?: 0
But agree that default implementation of NEL would be nice addition for stdlib
1
d