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.

what to do about Comparable

See original GitHub issue

After #7395, I changed Comparable to be invariant.

@jvasileff points out that a bunch of our functions including sort() and max() don’t work as well when you have things that indirectly implement comparable. It is possible to rescue all these functions by adding a new type parameter C and having parameters of type, for example, Element & Comparable<C> or {Element & Comparable<C>*}.

However, that’s pretty messy and I don’t see it as acceptable. There’s two reasonable solutions:

  1. Make Comparable contravariant and remove its self type.

  2. Taking advantage of #7408, define Comparable as follows:

    shared interface Comparable<Other> of Other 
            given Other satisfies Comparable<in Other> { ... }
    

    Then functions like sort() and max() just use the same type constraint given Other satisfies Comparable<in Other>.

I like solution 2, assuming that #7408 doesn’t run into some terrible snag.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
gavinkingcommented, Sep 15, 2018

After sleeping on this, I think the cleanest solution is just to:

  1. Make Comparable contravariant and remove its self type.

So I’ve done that. Thanks to @jvasileff for pointing out the problems with making Comparable invariant.

0reactions
gavinkingcommented, Sep 17, 2018

Right.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparable Interface in Java with Examples - GeeksforGeeks
The Comparable interface is used to compare an object of the same class with an instance of that class, it provides ordering of...
Read more >
When to use Comparable and Comparator - Stack Overflow
Comparator can be used to compare instances of different classes. Comparable is implemented by the class which needs to define a natural ordering...
Read more >
How to Stop Comparing Yourself to Others - Ramsey Solutions
1. Start your day by writing out three things you can be grateful for today. 2. Develop a peace about your life and...
Read more >
Comparable (Java Platform SE 8 ) - Oracle Help Center
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's...
Read more >
Real Estate Comps: How To Find & Use Them
Real estate comps are similar homes in the same neighborhood. Here's how to find these comparable houses and the smartest way to use...
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