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

Implicit catsKernelOrderingForOrder is not very useful #4332

Open
armanbilge opened this issue Oct 31, 2022 · 7 comments
Open

Implicit catsKernelOrderingForOrder is not very useful #4332

armanbilge opened this issue Oct 31, 2022 · 7 comments

Comments

@armanbilge
Copy link
Member

trait OrderToOrderingConversion {
/**
* Implicitly derive a `scala.math.Ordering[A]` from a `Order[A]`
* instance.
*/
implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] =
ev.toOrdering
}

The problem is, when the compiler searches for an implicit Ordering[A], it has no reason to check the Order companion object. So it would have to be manually brought into scope e.g. import cats.kernel.Order._ or something.

@johnynek
Copy link
Contributor

yeah, but if you want it you would import it right?

I think it would be dangerous to put it in cats.implicits._ although maybe it would be okay. I worry it could create ambiguous implicits and then look like no Ordering could be found.

@armanbilge
Copy link
Member Author

armanbilge commented Oct 31, 2022

The cats.implicits._ import is deprecated now anyway, so I didn't really consider that an option.

Also cats.kernel.Order._ seems like a bad idea, because the wildcard will import lots of random stuff.

So actually the only realistic way to use this is to import cats.kernel.Order.catsKernelOrderingForOrder. All so you can avoid doing this somewhere in your code:

implicit val ordering = Order[A].toOrdering

Maybe that's fine and it's a matter of taste. But I took the latter option instead of the bulky import.

Edit: linking to

@johnynek
Copy link
Contributor

when was cats.implicits._ deprecated? Oh it's that cats.syntax._ or something replacement right?

@armanbilge
Copy link
Member Author

Yup, that's right, it's cats.syntax.all._ now.

https://github.com/typelevel/cats/releases/tag/v2.2.0

In most cases all that's necessary to switch to using the new implicit scope instances is to replace cats.implicits._ imports with cats.syntax.all._ and delete any cats.instances imports

@satorg
Copy link
Contributor

satorg commented Oct 31, 2022

import cats.kernel.Order.Implicits._

maybe or something like that?

Not too cool, but still better than just

import cats.kernel.Order._

@armanbilge
Copy link
Member Author

armanbilge commented Aug 21, 2023

I think it would be dangerous to put it in cats.implicits._ although maybe it would be okay.

Chatting on Discord today Fabio points out that this instance is already in cats.implicits._. In fact, it's one of the few cases where the implicits import can't be replaced by the syntax import.

@satorg
Copy link
Contributor

satorg commented Aug 21, 2023

import cats.syntax.all._ can still be used in combination with
import cats.kernel.instances.order._,
but it is not the best experience in terms of API discoverability of course.

UPD: changed "it is the best experience" to "NOT the best experience". Keep mistyping, sorry.

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

3 participants