Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Suggestion: Replace/remove all "unsafe" toNonEmptyFoo extensions and rename the nullable ones #3

Closed
ragnese opened this issue Mar 30, 2021 · 2 comments

Comments

@ragnese
Copy link
Contributor

ragnese commented Mar 30, 2021

For example, you have:

fun <T> List<T>.nonEmptyOrNull() = if (isEmpty()) null else toNonEmptyList()

and

@UnsafeNonEmptyCollectionApi
fun <T> Iterable<T>.toNonEmptyList(): NonEmptyList<T> = isAlreadyNonEmptyOr {
    nonEmptyListOf(first(), drop(1))
}

If it were me, I'd probably be lazy and just drop the whole unsafe set. The user can always just do:

val nonEmpty = someList.nonEmptyOrNull() ?: error("Bug! I expected this to be non-empty!")

But if you keep both APIs, I suggest at least naming both sets with the "toNonEmptyFoo" style. A lot of methods in the Kotlin standard library have names like toList, so going with toNonEmptyList seems like a good match.

@balazstothofficial
Copy link
Contributor

Good point 👍🏻
Please have a look at: #4

@ragnese
Copy link
Contributor Author

ragnese commented Mar 31, 2021

I like it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants