Skip to content

Weaken some Alternative constraints in OneAnd #4739

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kory33
Copy link

@kory33 kory33 commented Apr 20, 2025

Currently, the unwrap and combine methods in OneAnd, as well as SemigroupK / Semigroup instance have Alternative as a bound for the type F[_] of collection-like component.

This pull request weakens this constraint to F[_]: NonEmptyAlternative.

Context

The change allows one to unwrap OneOf[NonEmptyList, A] into NonEmptyList[A]. At first glance putting NonEmptyList in OneOf sounds a bit weird, but I think they are natural choices for situations when one wants to describe a collection of 2..N items (this occurred to me while describing "duplicate inputs error" in a validation logic, where the error case class had a length ≥2 list of input indices containing duplicates).

I think this weakening generalization makes sense since, intuitively, none of the aforementioned methods interact with the empty: F[Nothing] aspect of the collection: they only combine collections (SemigroupK) or inject an element into the collection (NonEmptyAlternative).

@@ -123,8 +123,21 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) {
s"OneAnd(${A.show(head)}, ${FA.show(tail)})"
}

private[data] trait OneAndBinCompat0[F[_], A] {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] Is this the right way to pass the MiMa check for a case class modification?

I saw #3997 (comment) and thought that this was the (only) way, but I could not find any other place that defines a similar bincompat trait (other than ValidatedFunctionsBinCompat0, which patches the companion rather than the Validated class itself).

val head: A
val tail: F[A]

@deprecated("Kept for binary compatibility", "2.14.0")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] Is since = "2.14.0" specification appropriate (is that the next cats version)? Or do I just not need @deprecated at all since it's private[data]?

@kory33
Copy link
Author

kory33 commented Apr 20, 2025

Oh, I thought I made the bincompat check pass but I didn't! I'll try to fix 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

Successfully merging this pull request may close these issues.

1 participant