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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:06:46+00:00 2026-06-07T19:06:46+00:00

Consider this simple binary tree type: abstract class BinaryTree[T] { type Repr <: BinaryTree[T]

  • 0

Consider this simple binary tree type:

abstract class BinaryTree[T] {
  type Repr <: BinaryTree[T]

  def left: Repr
  def value: T
  def right: Repr

  def height: Int

 def add(elem: T): Repr = 
    if ( left.height <= right.height ) copy( left.add( elem ), right)
    else copy( left, right.add(elem))

 def copy( left: Repr, right: Repr ) : Repr
}

abstract class BinarySearchTree[T] extends BinaryTree[T] {
  type Repr <: BinarySearchTree[T]

  def ordering: Ordering[T]

  override def add(elem: T) = 
    if ( ordering.equiv( elem, value ) ) this.asInstanceOf[Repr]
    else if ( ordering.lt(elem, value) ) copy( left.add( elem ), right )
    else copy( left, right.add( elem ) )
}


class ScapegoatTree[T]( val value: T, val left: ScapegoatTree[T], val right: ScapegoatTree[T] )(implicit val ordering: Ordering[T])
  extends BinarySearchTree[T] {

  type Repr = ScapegoatTree[T]

  def add = this /* snip */

  def copy( left: ScapegoatTree[T], right: ScapegoatTree[T] ) = new ScapegoatTree( value, left, right )
}

class BalancedBinaryTree[T]( val value: T, val left: BalancedBinaryTree[T], val right: BalancedBinaryTree[T] )(implicit val ordering: Ordering[T])
  extends BinarySearchTree[T] {

  type Repr = BalancedBinaryTree[T]

  def add = this /* snip */

  def copy( left: BalancedBinaryTree[T], right: BalancedBinaryTree[T] ) = new BalancedBinaryTree( value, left, right )
}

Using Scala 2.9 in Scala-IDE, the add method fails to compile because copy takes a Repr while left.add(elem) and right.add(elem) return a Repr#Repr. As far as I can tell, Repr#Repr must always be a subtype of Repr, and therefore it should be safe. Have I failed to consider some case, or is this one of those fortunately rare instances where the type system prevents you from doing something that ought to work?

Also, is there any way I could change my type definition so this would be legal, or assert to the compiler that Repr#Repr must be a subtype of Repr (aside from a cast, I mean). I’m still getting used to Scala and I’m not sure I’ve fully comprehended the type system yet. I did get it to compile by writing an implicit method that does a cast, but that feels like more of a workaround than a solution.

Thanks in advance for your help.

Edit: I should add that in my actual implementation of this I have subclasses that further restrict the type of Repr, if that makes a difference.

Edit 2: Added more code, as requested. This issue appears in the BinarySearchTree type as well, but not in concrete implementations such as ScapegoatTree where Repr is specified. There is also a concrete implementation of the basic BinaryTree, but I’m trying to keep it minimal so you don’t have to wade through too much unnecessary code.

  • 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-07T19:06:49+00:00Added an answer on June 7, 2026 at 7:06 pm

    Just tried a rewritten using type parameters instead of abstract types, hope it suite your need.

    trait BinaryTree[T, Repr <: BinaryTree[T, Repr]] {
      def left: Repr
      def value: T
      def right: Repr
    
      def height: Int
      def add(elem: T): Repr =  
        if ( left.height <= right.height ) copy( left.add( elem ), right)
        else copy( left, right.add(elem))
    
      def copy( l: Repr, r: Repr ) : Repr
    }
    
    trait BinarySearchTree[T, U<:BinarySearchTree[T,U]] extends BinaryTree[T, U] { self:U =>
      def ordering: Ordering[T]
    
      override def add(elem: T) = 
        if ( ordering.equiv( elem, value ) ) self
        else if ( ordering.lt(elem, value) ) copy( left.add( elem ), right )
        else copy( left, right.add( elem ) )
    }
    
    class ScapegoatTree[T]( val value: T, val left: ScapegoatTree[T], val right: ScapegoatTree[T] )(implicit val ordering: Ordering[T]) extends BinarySearchTree[T, ScapegoatTree[T]] {
      override def height = 1
      override def copy( left: ScapegoatTree[T], right: ScapegoatTree[T] ) = new ScapegoatTree( value, left, right )
    }
    
    class BalancedBinaryTree[T]( val value: T, val left: BalancedBinaryTree[T], val right: BalancedBinaryTree[T] )(implicit val ordering: Ordering[T]) extends BinarySearchTree[T, BalancedBinaryTree[T]]{
      override def height = 1
      override def copy( left: BalancedBinaryTree[T], right: BalancedBinaryTree[T] ) = new BalancedBinaryTree( value, left, right )
    }
    

    According to this question, you are encounting a ‘MyType’ problem.

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

Sidebar

Related Questions

Consider this simple example: template <class Type> class smartref { public: smartref() : data(new
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
consider this simple code {$APPTYPE CONSOLE} uses Rtti, SysUtils; type {$M+} TFoo = class
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this simple example - public class Person { private String name; private Date
Consider this simple AS3 class. package { import flash.display.Sprite; import flash.display.MovieClip; public class MySprite
Consider this simple :has_many relationship: class Basket < ActiveRecord::Base has_many :apples ... end class
Consider this simple Relation MyRel(A, B) I wanted to display A based on value
Consider this simple example. Class A { B b; A() { this.b = new
Consider this simple class that demonstrates RAII in C++ (From the top of my

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.