-
Notifications
You must be signed in to change notification settings - Fork 451
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
Implement basic NonEmptySet #2899
Conversation
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job! I think this just need a few more tests to ensure that the implementation is correct even if somebody else changes or tries to optimize it even further.
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptySetTest.kt
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
@ibado I'm wondering whether the @JvmInline public value class NonEmptySet<out T> private constructor(
val elements: Set<T>
): AbstractSet<T> {
public constructor(first: T, varargs rest: T): this(setOf(first) + rest.toSet())
} You can still overload the public operator fun plus(s: Set<@UnsafeVariance T>): NonEmptySet<T> =
NonEmptySet(elements + s) This has the added benefit of completely removing the wrapper at runtime, since it's a value class. @roomscape what do you think? |
I think you're right, the In that case, your suggestion of a simple wrapper class works very well. Interface delegation would be a good fit: class NonEmptySet<out T> private constructor(elements: Set<T>): Set<T> by elements I like the idea of making it an inline class too, but I'm cautious about the implications, and I think the benefits would only be slight. Value classes can use interface delegation, so that would work. But value classes do seem to have some quirks, and in particular I think generic parameters are still experimental. It might be safer to leave that for a future enhancement. |
2b0dc4b
to
5da450b
Compare
I uploaded a simplify version following the approach of @roomscape since I think is nice to delegate the "set" capabilities to the wrapped set. Not sure about the inline value class.. It's up to you. |
5da450b
to
791e86b
Compare
791e86b
to
865393d
Compare
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
@ibado I've been working on a similar PR for Once you do this, you can really change to a value class, which removes any wrapper at runtime: @JvmInline public value class NonEmptySet<out T> private constructor(
private val elements: Set<T>
) : Set<T> by elements { |
@serras Done! the equals was already there, but I was not checking for |
Now |
@ibado I think the commit I've just pushed should fix the problem (it's just a different way to write the generator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @ibado! Thank you and congrats on your first contribution 🙌 🥳
Small styling nit.
I think this just need a few more tests to ensure that the implementation is correct
This would also be nice, you can run ./gradlew koverHtmlReport
to check the coverage of NonEmptySet
.
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
Outdated
Show resolved
Hide resolved
@ibado congrats on your first contribution! Thanks for your work here, and for ensuring the best quality for this new implementation :) |
Thanks! Happy to help 😄 |
This is a super basic implementation for #2565. After review, if this is fine, I can add any other functionality may be required.