Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:34:45+00:00 2026-06-05T20:34:45+00:00

I am trying to define an abstract class that has operators to compare two

  • 0

I am trying to define an abstract class that has operators to compare two instances of the class. When concretizing the class, however, I want the methods to only compare instances of the same type. Something like this

abstract class ComparableSuper{
  def <(other: ComparableSuper): Boolean  
  def <=(other: ComparableSuper): Boolean  
  def >(other: ComparableSuper): Boolean
  def >=(other: ComparableSuper): Boolean
}


class Comparable (val a: Int) extends ComparableSuper {
   def <(other: Comparable): Boolean = this.a < other.a
   def >(other: Comparable): Boolean = this.a > other.a
   def <=(other: Comparable): Boolean = this.a <= other.a
   def >=(other: Comparable): Boolean = this.a >= other.a
}

Of course this code does not compile because I am not overriding the methods in the abstract class. If I change Comparable to ComparableSuper in the methods, however, I won’t have the assurance that the field a is there.

Is there a way I could specify the type of the class in the method signature?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:34:46+00:00Added an answer on June 5, 2026 at 8:34 pm

    I highly recommend you to look at Ordering type class.

    I find, that type-class approach to this problem is much better than Comaprable way of doing it (where objects, that you want to compare, actually extend Comparable).

    With type-class approach you will also give you more type-safety and flexibility. You can actually define Ordering instances for existing classes (classes that you can’t control and change).


    Ordering can be a little bit clumsy to use, but Ordered has implicits that will allow you to write code like this:

    import math.Ordered._
    
    def isGreater[T : Ordering](a: T, b: T) = a > b
    

    So you even don’t compromise convenience. (by the way, Ordered is equivalent to Java’s Comparable)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to establish an abstract class. I want to ensure that any
I am trying to build a simple abstract Cache class that can enforce methods
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I am trying to write an abstract class to manage managed entities. However, I
I have an abstract class, AbsClass that implements an interface, IClass . IClass has
is that possible create an abstract class from DbQuery and IDbSet? I'm trying to
I'm trying to create an abstract class that defines a property with a getter.
I want to define an abstract base class with a vector of struct variables,
I am trying to define an abstract class implementing Comparable. When I define the
I'm trying to define a generic class public abstract class RepositoryBase<TDatabase, TKey, T> :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.