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 6820059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:22:47+00:00 2026-05-26T21:22:47+00:00

I am making a basic graph representation in Scala. abstract class Vertex class Edge

  • 0

I am making a basic graph representation in Scala.

abstract class Vertex
class Edge (
  val source: Vertex
  val dest: Vertex
)
class Graph[V <: Vertex] {
  ...
}

At some point, I need to be able to sort a list of vertices in Graph[Vertex]. I would like to do so by calling vertices.sorted. If I make Vertex extend Ordered[Vertex] then I don’t actually have the right compareTo since the ordering information is going to be data in the implemented subclass of Vertex and compareTo takes any Vertex. But if I make my Vertex implementation MyVertex extend Ordered[MyVertex], then that information is not available in Graph[MyVertex].

What is the best solution to this problem? Should I be using an implicit Ordering? Is there a way to enforce that subclasses of Vertex have an implicit Ordering on themselves?

I would rather not do the following:

class MyClass extends Vertex {
  override def compare(that: Vertex) = that match {
    case that: MyClass => // do the comparison
    case _ => false
  }
}

Update: maybe Graph‘s constructor needs to take an Ordering[V]?

Update: I could always restrict V <: Ordered[V] but this does not scale. Is it possible to have multiple type restrictions?

  • 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-05-26T21:22:47+00:00Added an answer on May 26, 2026 at 9:22 pm

    Update: maybe Graph‘s constructor needs to take an Ordering[V]?

    That would be my preferred approach, because this way you may pass a different Ordering if needed.

    Update: I could always restrict V <: Ordered[V] but this does not scale. Is it possible to have multiple type restrictions?

    Yes, since Scala has intersection types: V <: Ordered[V] with Something[V] with SomethingElse[V].

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

Sidebar

Related Questions

I am making a very basic webpage, consisting of some text inside a fixed
I need just a basic example or tutorial of making a mobile phone and
I am making a mini CMS for two sites and am storing some basic
I'm making a basic GPS. I want to put the location listener class in
I'd like to parse Python source in order to try making a basic source
I am making a basic PHP source viewer for my blog's example code folder.
I've been making some very basic MVC views to save some data. To date
I am making a basic point n' click game and I came upon this
I am making a basic Tiff viewer and need to be able to select
I am making a basic program which takes some input and prints it back

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.