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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:36:23+00:00 2026-05-18T08:36:23+00:00

I couldn’t find the answer to this in any other question. Suppose that I

  • 0

I couldn’t find the answer to this in any other question. Suppose that I have an abstract superclass Abstract0 with two subclasses, Concrete1 and Concrete1. I want to be able to define in Abstract0 something like

def setOption(...): Self = {...}

where Self would be the concrete subtype. This would allow chaining calls to setOption like this:

val obj = new Concrete1.setOption(...).setOption(...)

and still get Concrete1 as the inferred type of obj.

What I don’t want is to define this:

abstract class Abstract0[T <: Abstract0[T]]

because it makes it harder for clients to handle this type. I tried various possibilities including an abstract type:

abstract class Abstract0 {
  type Self <: Abstract0
}

class Concrete1 extends Abstract0 {
  type Self = Concrete1
}

but then it is impossible to implement setOption, because this in Abstract0 does not have type Self. And using this: Self => also doesn’t work in Abstract0.

What solutions are there to this issue?

  • 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-18T08:36:23+00:00Added an answer on May 18, 2026 at 8:36 am

    This is what this.type is for:

    scala> abstract class Abstract0 {
         |   def setOption(j: Int): this.type
         | }
    defined class Abstract0
    
    scala> class Concrete0 extends Abstract0 {
         |   var i: Int = 0
         |   def setOption(j: Int) = {i = j; this}
         | }
    defined class Concrete0
    
    scala> (new Concrete0).setOption(1).setOption(1)
    res72: Concrete0 = Concrete0@a50ea1
    

    As you can see setOption returns the actual type used, not Abstract0. If Concrete0 had setOtherOption then (new Concrete0).setOption(1).setOtherOption(...) would work

    UPDATE: To answer JPP’s followup question in the comment (how to return new instances:
    The general approach described in the question is the right one (using abstract types). However, the creation of the new instances needs to be explicit for each subclass.

    One approach is:

    abstract class Abstract0 {
      type Self <: Abstract0
    
      var i = 0
    
      def copy(i: Int) : Self
    
      def setOption(j: Int): Self = copy(j)
    }
    
    class Concrete0(i: Int) extends Abstract0 {
      type Self = Concrete0
      def copy(i: Int) = new Concrete0(i)
    }
    

    Another one is to follow the builder pattern used in Scala’s collection library. That is, setOption receives an implicit builder parameter. This has the advantages that building the new instance can be done with more methods than just ‘copy’ and that complex builds can be done. E.g. a setSpecialOption can specify that the return instance must be SpecialConcrete.

    Here’s an illustration of the solution:

    trait Abstract0Builder[To] {
        def setOption(j: Int)
        def result: To
    }
    
    trait CanBuildAbstract0[From, To] {
      def apply(from: From): Abstract0Builder[To]
    }
    
    
    abstract class Abstract0 {
      type Self <: Abstract0
    
      def self = this.asInstanceOf[Self]
    
      def setOption[To <: Abstract0](j: Int)(implicit cbf: CanBuildAbstract0[Self, To]): To = {
        val builder = cbf(self)
        builder.setOption(j)
        builder.result
      }
    
    }
    
    class Concrete0(i: Int) extends Abstract0 {
      type Self = Concrete0
    }
    
    object Concrete0 {
        implicit def cbf = new CanBuildAbstract0[Concrete0, Concrete0] {
            def apply(from: Concrete0) = new Abstract0Builder[Concrete0] {
               var i = 0
               def setOption(j: Int) = i = j
               def result = new Concrete0(i)
            }
        }
    }
    
    object Main {
        def main(args: Array[String]) {
        val c = new Concrete0(0).setOption(1)
        println("c is " + c.getClass)
        }
    }
    

    UPDATE 2:
    Replying to JPP’s second comment. In case of several levels of nesting, use a type parameter instead of type member and make Abstract0 into a trait:

    trait Abstract0[+Self <: Abstract0[_]] {
      // ...
    }
    
    class Concrete0 extends Abstract0[Concrete0] {
      // ....
    }
    
    class RefinedConcrete0 extends Concrete0 with Abstract0[RefinedConcrete0] {
     // ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Couldn't find an answer to this one. I have a WPF ListView control that
Couldn't find an answer to this question. It must be obvious, but still. I
Couldn't seem to find the answer to this question - what is the haml
Couldn't find a better way to phrase this question. Here's the deal: I have
Couldn't find any answer to this problem, or not even any questions asked. So
I couldn't find anything similar to this anywhere. I have an array of pointers
I couldn't find this in the documentation. If I have a complex object in
I couldn't find a straight answer to my question and need to know it
I couldn't find any useful information on Microsoft's site, so here is the question:
Couldn't find an answer to this solution, so once I figured it out I

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.