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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:11:26+00:00 2026-05-15T06:11:26+00:00

Suppose one wants to build a novel generic class, Novel[A] . This class will

  • 0

Suppose one wants to build a novel generic class, Novel[A]. This class will contain lots of useful methods–perhaps it is a type of collection–and therefore you want to subclass it. But you want the methods to return the type of the subclass, not the original type. In Scala 2.8, what is the minimal amount of work one has to do so that methods of that class will return the relevant subclass, not the original? For example,

class Novel[A] /* What goes here? */ {
  /* Must you have stuff here? */
  def reverse/* What goes here instead of :Novel[A]? */ = //...
  def revrev/*?*/ = reverse.reverse
}
class ShortStory[A] extends Novel[A] /* What goes here? */ {
  override def reverse: /*?*/ = //...
}
val ss = new ShortStory[String]
val ss2 = ss.revrev  // Type had better be ShortStory[String], not Novel[String]

Does this minimal amount change if you want Novel to be covariant?

(The 2.8 collections do this among other things, but they also play with return types in more fancy (and useful) ways–the question is how little framework one can get away with if one only wants this subtypes-always-return-subtypes feature.)

Edit: Assume in the code above that reverse makes a copy. If one does in-place modification and then returns oneself, one can use this.type, but that doesn’t work because the copy is not this.

Arjan linked to another question that suggests the following solution:

def reverse: this.type = {
  /*creation of new object*/.asInstanceOf[this.type]
}

which basically lies to the type system in order to get what we want. But this isn’t really a solution, because now that we’ve lied to the type system, the compiler can’t help us make sure that we really do get a ShortStory back when we think we do. (For example, we wouldn’t have to override reverse in the example above to make the compiler happy, but our types wouldn’t be what we wanted.)

  • 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-15T06:11:27+00:00Added an answer on May 15, 2026 at 6:11 am

    I haven’t thought this through fully, but it type checks:

    object invariant {
      trait Novel[A] {
        type Repr[X] <: Novel[X]
    
        def reverse: Repr[A]
    
        def revrev: Repr[A]#Repr[A]
           = reverse.reverse
      }
      class ShortStory[A] extends Novel[A] {
        type Repr[X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    
    object covariant {
      trait Novel[+A] {
        type Repr[X] <: Novel[_ <: X]
    
        def reverse: Repr[_ <: A]
    
        def revrev: Repr[_ <: A]#Repr[_ <: A] = reverse.reverse
      }
    
      class ShortStory[+A] extends Novel[A] {
        type Repr[X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    

    EDIT

    The co-variant version can be much nicer:

    object covariant2 {
      trait Novel[+A] {
        type Repr[+X] <: Novel[X]
    
        def reverse: Repr[A]
    
        def revrev: Repr[A]#Repr[A] = reverse.reverse
      }
    
      class ShortStory[+A] extends Novel[A] {
        type Repr[+X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen second one in another's code and I suppose this length comparison have
Suppose I have one long long int and want to take its bits and
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Suppose I have one html page with frames. The left frame is simply a
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -
Suppose we have an iterator (an infinite one) that returns lists (or finite iterators),
Suppose I have a process which spawns exactly one child process. Now when the
I suppose it allows for moving changes from one branch to the next but
Suppose that two tables exist: users and groups . How does one provide simple
Suppose you have a file that contains IP addresses, one address in each line:

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.