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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:00:07+00:00 2026-05-17T15:00:07+00:00

Update : Clarified and expanded, since the original question was simplified too far I

  • 0

Update: Clarified and expanded, since the original question was simplified too far

I need a pair of traits, each of which refers to the other such that parent and child classes must relate to each other.

trait Parent [C <: Child] {
  def foo(c: C)
}

trait Child [P <: Parent] {
  def parent: P = ...
  def bar = parent.foo(this)
}

Such that implementing classes must come in pairs:

class ActualParent extends Parent [ActualChild] {
  def foo(c: ActualChild) = ...
}

class ActualChild extends Child [ActualParent] {
}

Unfortunately, the compiler doesn’t like these traits because the generic types aren’t complete. Instead of C <: Child it needs to say C <: Child[something]. Leaving them unspecified doesn’t work either:

trait Parent [C <: Child[_]] {
  def foo(c: C)
}

trait Child [P <: Parent[_]] {
  def parent: P = ...
  def bar = parent.foo(this)
}

It now complains on the parent.foo(this) line, because it doesn’t know that this is of the correct type. The type of parent needs to be Parent[this.type] for the call to foo to have the right types.

I’m thinking there must be a way of referring to an object’s own type? Or to a type that’s required to be itself?


Update: Further to @Daniel’s answer, I tried using an abstract type member within the child to state the generic types of the parent type like this:

trait Parent [C <: Child] {
  def foo(c: C)
}

trait Child {
  type P <: Parent[this.type]

  def parent: P = ...
  def bar = parent.foo(this)
}

This isn’t working when I try to implement it:

class ActualParent extends Parent [ActualChild] {
  def foo(c: ActualChild) = ...
}

class ActualChild extends Child {
  type P = ActualParent
}

Gives the following error:

overriding type Parent in trait Child with bounds >: Nothing <: Parent[ActualChild.this.type]
type Parent has incompatible type

What does that mean?

  • 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-17T15:00:07+00:00Added an answer on May 17, 2026 at 3:00 pm

    You can use the approach given in http://programming-scala.labs.oreilly.com/ch13.html:

    abstract class ParentChildPair {
      type C <: Child
      type P <: Parent
    
      trait Child {self: C =>
        def parent: P
      }
    
      trait Parent {self: P =>
        def child: C
      }
    }
    
    class ActualParentChildPair1 {
      type C = Child1
      type P = Parent1
    
      class Child1 extends Child {...}
    
      class Parent1 extends Parent {...}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: clarified question (I hope) Hi. I'm developing a plugin in Wordpress and I'm
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
Update: Please read this question in the context of design principles, elegance, expression of
The question was about plain c functions, not c++ static methods, as clarified in
UPDATE - No need to answer this now, I have solved below. Hi, I'm
UPDATE : Thanks for all the quick responses. Should have probably clarified that I've
Update: Solved, with code I got it working, see my answer below for the
Update: giving a much more thorough example. The first two solutions offered were right
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to

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.