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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:53:14+00:00 2026-05-13T07:53:14+00:00

I ran into a problem using case classes and parameterized types with an upper

  • 0

I ran into a problem using case classes and parameterized types with an upper type bound.
The Scala compiler tells me that it finds type DefaultEdge but requires Edge[Type]. I tried using something like case DefaultEdge[Type] but I get syntax errors.

Here is my setup. I have a couple of edge case classes which correspond to different types. Those classes contain the parameterized type V.

object EdgeKind extends Enumeration {
  type EdgeKind = Value
  val Default, Jump, True, False, DefaultCase, Case, Throw, Return = Value
}

sealed abstract class Edge[V <: VertexLike](val startVertex: V, val endVertex: V, val kind: EdgeKind.EdgeKind)

case class DefaultEdge[V <: VertexLike](override val startVertex: V, override val endVertex: V)
  extends Edge[V](startVertex, endVertex, EdgeKind.Default)
case class JumpEdge[V <: VertexLike](//...

Then I have a trait called GraphLike which defines a couple of methods. The only interesting part should be this one:

trait GraphLike[V <: VertexLike] {
  protected type E <: Edge[V]
}

Another trait in between implements some methods of the GraphLike trait and is called GraphLikeWithAdjacencyMatrix. When I wire everything together I have the following class:

class CFG extends GraphLikeWithAdjacencyMatrix[BasicBlockVertex] {
  def dotExport = {
    def vertexToString(vertex: BasicBlockVertex) = ""
    def edgeToString(edge: E) = edge match {//also tried Edge[BasicBlockVertex] here
      case DefaultEdge => error("CFG may not contain default edges.")
      case JumpEdge => "jump"
      case TrueEdge => "true"
      case FalseEdge => "false"
      case DefaultCaseEdge => "default"
      case CaseEdge => "case"
      case ThrowEdge => "throw"
      case ReturnEdge => "return"
    }
    new DOTExport(this, vertexToString, edgeToString)
  }
}

This is where I run into the problems. I get told that Edge[BasicBlockVertex] is expected and I only provide a DefaultEdge. The definition in DOTExport is class DOTExport[V <: VertexLike](val graph: GraphLike[V], val vertexToString: V => String, val edgeToString: Edge[V] => String)

So my question is now, how could I still use case classes for the edge types and make the compiler happy? It must be some stupid mistake on my side.

By the way, the match-code works once I say DefaultEdge(x,y) instead of DefaultCase etc. However then the instantiation of DOTExport fails because Edge[?] is required and I pass a CFG.E

Thank you!

EDIT: In fact the combination of E = Edge[V] in GraphLike and using DefaultEdge(_, _) works. This is unfortunately just the result of try and error. I would really like to know why it works now.

The error message:

(fragment of test.scala):25: error:
type mismatch;  found   :
(Graph.this.E) => java.lang.String 
required: (this.Edge[?]) => String
    new DOTExport(this, (vertex: V) => vertex.toString, edgeToString)

Here is the full compilable code illustrating my problem. Again, my problem is line 14, since everything works when you replace type E <: Edge[V] with type E = Edge[V] and I have no idea why.

object EdgeKind {
  val Default = 0
  val Jump = 1
}

abstract class Edge[V <: VertexLike](val startVertex: V, val endVertex: V, val kind: Int)

case class DefaultEdge[V <: VertexLike](override val startVertex: V, override val endVertex: V) extends Edge[V](startVertex, endVertex, EdgeKind.Default)
case class JumpEdge[V <: VertexLike](override val startVertex: V, override val endVertex: V) extends Edge[V](startVertex, endVertex, EdgeKind.Jump)

trait VertexLike

trait GraphLike[V <: VertexLike] {
  protected type E <: Edge[V] // Everything works when E = Edge[V]
}

class DOTExport[V <: VertexLike](val graph: GraphLike[V], val vertexToString: V => String, val edgeToString: Edge[V] => String)

class Graph[V <: VertexLike] extends GraphLike[V] {
  def dotExport = {
    def edgeToString(edge: E) = edge match {
      case DefaultEdge(_, _) => ""
      case JumpEdge(_, _) => "jump"
    }
    new DOTExport(this, (vertex: V) => vertex.toString, edgeToString)
  }
}
  • 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-13T07:53:14+00:00Added an answer on May 13, 2026 at 7:53 am

    There’s too much missing to be able to really help. You must provide the exact error messages, instead of paraphrasing them.

    At any rate, case DefaultEdge means a comparision between the object passed and the object DefaultEdge. The latter is the object companion of the class DefaultEdge, automatically created through the use of the case class statement. Such companion objects do not belong to the class they are companion to. They are singletons, which means their own classes are unique to themselves, and, otherwise, just inherit AnyRef.

    So, in other words, DefaultEdge is not an Edge, and that’s why you get an error. As for the error you got when you used DefaultEdge(_, _), you ommitted too much detail. However… are you sure you wrote the code that way? I would expect the following instead:

    new DOTExport(this, vertexToString _, edgeToString _)
    

    EDIT

    Ok, the second error message is clear now. The original declaration of E was that it was a subclass of Edge, but DOTExport is expecting a function that takes an Edge and converts it into a String. To understand the problem here, note that the following definition also works:

    protected type E >: Edge[V]
    

    Let’s say, to illustrate the problem, that you have two subclasses of Edge: IntEdge and StringEdge. The first has a number field, and the second a name field. So, we could write the following functions:

    def intEdgeToString(ie: IntEdge) = ie.number.toString
    def stringEdgeToString(se: StringEdge) = se.name
    

    Now, let’s create a var and store one of them:

    var eTS: E => String = intEdgeToString _
    

    Since E is any subclass of Edge, this would be acceptable. So we create a DOTExport passing eTS to it. Next, we feed DOTExport not with IntEdge, but with StringEdge. Since the latter do not have a number field, trying to run it would cause an exception at run-time, which defeats the whole purpose of static typing.

    It is to prevent this kind of problem that Scala did not accept your original definition.

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

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'll try three guesses. 1) You have some explicit code… May 14, 2026 at 6:52 pm
  • Editorial Team
    Editorial Team added an answer Try Not IN AND tablea.id NOT In (SELECT ID FROM… May 14, 2026 at 6:52 pm
  • Editorial Team
    Editorial Team added an answer <ClientSideEvents EndCallback="function(s,e) { clientCADIncidentNo.mainElement.title=s.cpUnReconcileCADIncidentNo; }" /> where cpUnReconcileCADIncidentNo is the… May 14, 2026 at 6:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.