Skip to content

Don't require Order for the value on Cogen for SortedMap and NonEmptyMap#4296

Merged
armanbilge merged 3 commits intotypelevel:mainfrom
cquiroz:cogen-nonempty-map
Oct 10, 2022
Merged

Don't require Order for the value on Cogen for SortedMap and NonEmptyMap#4296
armanbilge merged 3 commits intotypelevel:mainfrom
cquiroz:cogen-nonempty-map

Conversation

@cquiroz
Copy link
Copy Markdown
Contributor

@cquiroz cquiroz commented Sep 11, 2022

It seems we don't need to require an Order instance for Cogen of SortedMap and NonEmptyMap. We only need it for the keys

Fixes #4295

Comment on lines 133 to 134

implicit def cogenNonEmptyMap[K: Order: Cogen, A: Order: Cogen]: Cogen[NonEmptyMap[K, A]] =
implicit def cogenNonEmptyMap[K: Order: Cogen, A: Cogen]: Cogen[NonEmptyMap[K, A]] =
Cogen[SortedMap[K, A]].contramap(_.toSortedMap)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For binary compatibility we have to keep the old signature as well, like this:

@deprecated("Preserved for bincompat", "2.9.0")
def cogenNonEmptyMap[K, A](kOrder: Order[K], kCogen: Cogen[K], aOrder: Order[A], aCogen: Coge[A]): Cogen[NonEmptyMap[K, A]]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean copy the old signature and make it non implicit?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes, exactly.

@armanbilge armanbilge changed the title Don't require Order for the value on Cogen for SortedMap and NonEmptyMap Don't require Order for the value on Cogen for SortedMap and NonEmptyMap Sep 11, 2022
@armanbilge armanbilge added this to the 2.9.0 milestone Sep 11, 2022
@armanbilge armanbilge added the bug label Sep 11, 2022
@cquiroz
Copy link
Copy Markdown
Contributor Author

cquiroz commented Sep 11, 2022

Added delegation as requested

Copy link
Copy Markdown
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

cogenNonEmptyMap[K, A]
}

implicit def cogenNonEmptyMap[K: Order: Cogen, A: Cogen]: Cogen[NonEmptyMap[K, A]] =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm confused why we even need the K: Order here. A SortedMap knows the Ordering[K].

Doesn't this also work only with K: Cogen, A: Cogen?

Copy link
Copy Markdown
Member

@armanbilge armanbilge Sep 12, 2022

Choose a reason for hiding this comment

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

I was confused about this as well :) Scalacheck implements the Cogen[Map] by transforming it to an ordered collection.

https://github.com/typelevel/scalacheck/blob/7bb1a47af2313f7e7935d34cce255f7b7c0b1b31/core/shared/src/main/scala/org/scalacheck/Cogen.scala#L129-L130

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch! As a side note: in fact, there are quite a lot of places in the Cats codebase where a constraint is demanded but not actually used. So this is why I think it would be great to have "unused" warnings enforced eventually.

@satorg satorg self-requested a review September 15, 2022 15:19
@armanbilge
Copy link
Copy Markdown
Member

@satorg did you want to re-review this?

@satorg
Copy link
Copy Markdown
Contributor

satorg commented Oct 10, 2022

Oh, I thought that Oscar asked a question and we're awaiting an answer from @cquiroz ...
But now I've realized that the concern had been cleared, hadn't it?

@armanbilge
Copy link
Copy Markdown
Member

Yes, I think this one is ready for merge :)

@armanbilge armanbilge merged commit 3ffee33 into typelevel:main Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unnecessary Order param for SortedMap/NonEmptyMap Cogen

4 participants