|
| 1 | +# NonEmptyCollections: Make the world a little more type-safe 🌍🏆 |
| 2 | +Reduce the need for emptiness checks and reduce unsafe APIs with **NonEmptyCollections**. |
| 3 | + |
| 4 | +You can use `NonEmptyList`s, `NonEmptySet`s and `NonEmptyMap`s to restrict the input of functions to make your code safer and avoid unnecessary runtime exceptions. |
| 5 | + |
| 6 | +For a detailed explanation see our related [article](--link--). |
| 7 | + |
| 8 | +This is an early version and work in progress. Do not hesitate to give feedback, ideas or improvements via an issue. |
| 9 | + |
| 10 | +# Examples |
| 11 | + |
| 12 | +## Average without exceptions |
| 13 | + |
| 14 | +With the `NonEmptyList` type, we can make sure that at least one element is always a list. If we want to calculate the average of that list, it is impossible to compile a program where an invalid input is passed to our function. |
| 15 | + |
| 16 | +```kotlin |
| 17 | +fun NonEmptyList<Int>.average() = sum() / size |
| 18 | +``` |
| 19 | + |
| 20 | +```kotlin |
| 21 | +nonEmptyListOf<Int>().average() // This does not comile!❌ |
| 22 | + |
| 23 | +nonEmptyListOf(1, 2, 3).average() // This does!✅ |
| 24 | +``` |
| 25 | + |
| 26 | +## Non-empty Shopping-Cart |
| 27 | + |
| 28 | +Let's imagine an online shop, where you can put articles into a shopping cart. If you have some articles in the shopping cart, you should be able to share the articles with your friends, save them for later on a wish list or directly buy them. But these three features just make sense if the shopping cart is not empty. Wouldn't it be cool to already prevent at compile-time that somebody tries these features with an empty set of articles? |
| 29 | + |
| 30 | +```kotlin |
| 31 | +sealed class ShoppingCart { |
| 32 | + object Empty : ShoppingCart() |
| 33 | + |
| 34 | + data class Filled( |
| 35 | + val articles: NonEmptySet<Article> |
| 36 | + ) : ShoppingCart() { |
| 37 | + |
| 38 | + fun buy(paymentType: PaymentType) = articles.buy(paymentType) |
| 39 | + fun share() = articles.share() |
| 40 | + fun saveTo(wishList: WishList) = articles.saveTo(wishList) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fun NonEmptyCollection<Article>.buy(paymentType: PaymentType) { 💸 } |
| 45 | + |
| 46 | +fun NonEmptyCollection<Article>.share() { 💬 } |
| 47 | + |
| 48 | +fun NonEmptyCollection<Article>.saveTo(wishList: WishList) { 💾 } |
| 49 | +``` |
| 50 | + |
| 51 | +The devs, who implement `buy`, `share` and `saveTo` don't have to handle the empty case. The consumers of these APIs don't have to think of exception handling, because they are forced by the compiler to provide a valid input. We would say, that's a win-win situation 🏆. |
| 52 | + |
| 53 | +# 🏃 Library Setup |
| 54 | +## 1. Add the repository |
| 55 | +`build.gradle.kts` |
| 56 | + |
| 57 | +```kotlin |
| 58 | +allprojects { |
| 59 | + repositories { |
| 60 | + ... |
| 61 | + maven { url = uri("https://jitpack.io") } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## 2. Add the dependency |
| 67 | +`build.gradle.kts` |
| 68 | + |
| 69 | +```kotlin |
| 70 | +dependencies { |
| 71 | + implementation(project("com.quickbirdstudios:nonEmptyCollection:1.0.0")) |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +# 👤 Author |
| 76 | +This Kotlin library is created with ❤️ by [QuickBird Studios](https://quickbirdstudios.com/). |
| 77 | + |
| 78 | +# ❤️ Contributing |
| 79 | +Open an issue if you need help, if you found a bug, or if you want to discuss a feature request. |
| 80 | + |
| 81 | +Open a PR if you want to make changes to NonEmptyCollections. |
| 82 | + |
| 83 | +# 📃 License |
| 84 | +NonEmptyCollections is released under an MIT license. See [License](LICENSE) for more information. |
0 commit comments