question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Be Consistent About Providing An Ordering Instance

See original GitHub issue

We sometimes have data types which subtype Ordered and other times have types which implement Ordering. We only need to do Ordering. We should ensure that all types which are Ordered also have an Ordering instance.

Context: https://github.com/http4s/http4s/pull/5013#discussion_r682655331

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
isomarctecommented, Sep 7, 2021

@san-coding if a class is already extending Ordered, we need to leave it that way, at least for now. This is so we don’t break binary compatibility.

In the context of this ticket, any type in http4s which is currently only extending Ordered should also have an implicit Ordering added, and they should be consistent with each other.

Hopefully, most of the types which are extending Ordered have a cats.kernel.Order instance. If they do, then adding the Ordering is very simple. For example,

final case class Foo(bar: Int) extends Ordered[Foo] {
    override final def compare(that: Foo): Int = 
      // Ordered derived from cats Order instance
      Order[Foo].compare(this, that)
}

object Foo {
    implicit val catsOrderInstanceForFoo: Order[Foo] = new Order[Foo] {
      override def compare(x: Foo, y: Foo): Int = x.bar.compare(y.bar)
    }
    
    // scala Ordering derived from cats Order
    implicit val stdLibOrderingInstance: Ordering[Foo] = catsOrderInstanceForFoo.toOrdering
}

If they don’t have a cats.kernel.Order instance (which you are free to add as well if you’d like, or not, either way), as in the case of HttpDate, then we’d do something like this.

class HttpDate private (val epochSecond: Long) extends Renderable with Ordered[HttpDate] { /* elided */ }

object HttpDate {
    implicit val stdLibOrderingInstance: Ordering[HttpDate] = /* Ordering definition */
}

For the sake of consistency, I’d also probably make the compare method from Ordered and the equals method (if it’s not a case class) use the underlying Ordering instance. Something like,

class HttpDate private (val epochSecond: Long) extends Renderable with Ordered[HttpDate] { 
  override def compare(that: HttpDate): Int = 
    Ordering[HttpDate].compare(this, that)
    
  override def equals(o: Any): Boolean = 
    o match {
      case that: HttpDate => Ordering[HttpDate].compare(this, that) === 0
      case _ => false
    }

That was a lot of text…let me know if that makes sense and don’t hesitate to reach out if you have questions.

0reactions
san-codingcommented, Sep 23, 2021

@isomarcte @rossabaker have opened a PR for adding Ordering instance QValue class

Read more comments on GitHub >

github_iconTop Results From Across the Web

member-ordering | typescript-eslint
Require a consistent member declaration order. This rule aims to standardize the way classes, interfaces, and type literals are structured and ordered. A...
Read more >
Why Comparable natural ordering needs to be consistent with ...
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted ...
Read more >
Consistency Guarantees in Distributed Systems Explained ...
Distributed systems are all about choosing the right trade-offs, similarly, we'll shortly see, consistency models are trade-offs between ...
Read more >
Frequently Asked Questions About Consistency at Work - Indeed
Consistency minimizes the chance of your work quality decreasing. Provide order in the workplace: If a workplace has several employees that ...
Read more >
Understanding Success Criterion 3.2.4: Consistent Identification
The intent of this Success Criterion is to ensure consistent identification of functional components ... Example 3: Consistent references to other pages.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found