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

The Archive Base Latest Questions

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

scala> class A defined class A scala> class B {this: A => } defined

  • 0
scala> class A
defined class A

scala> class B {this: A => }
defined class B

scala> new B
<console>:10: error: class B cannot be instantiated because it does not conform
to its self-type B with A
             new B
             ^

Class B sets the self type to class A, therefore class B (or a subclass of it) has to extend class A to create an instance of B. But is this possible at all, since a subclass of B can only extend one class (and this is class B) ?

So this leads me to the question, does it make sense to declare the self type of a class to another class in any case ?

  • 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-07T00:00:57+00:00Added an answer on June 7, 2026 at 12:00 am

    You are right with the observation that this definition can not lead to a concrete implementation, as you cannot mix two classes, only traits. So the short answer is ‘no’, either should be a trait.

    There are several questions on Stackoverflow regarding self-types. Two useful ones are these:

    • What is more Scala idiomatic: trait TraitA extends TraitB or trait TraitA { self: TraitB => }
    • Difference between trait inheritance and self type annotation

    In the second question, there is a good answer by Ben Lings who cites the following passage from the blog of Spiros Tzavellas:

    In conclusion, if we want to move method implementations inside traits then we risk polluting the interface of those traits with abstract methods that support the implementation of the concrete methods and are unrelated with the main responsibility of the trait. A solution to this problem is to move those abstract methods in other traits and compose the traits together using self type annotations and multiple inheritance.

    For example, if A (assume it’s a trait and not a class now!) is a logger. You don’t want to expose for B publicly the logging API mixed in by A. Therefore you would use a self-type and not mixin. Within the implementation of B you can call into the logging API, but from the outside it is not visible.


    On the other hand, you could use composition in the following form:

    trait B {
        protected def logger: A
    }
    

    The difference now is that

    • B must refer to logger when wanting to use its functionality
    • subtypes of B have access to the logger
    • B and A do not compete in namespace (e.g. could have methods of the same name without collision)

    I would say that self-types are a fairly peripheral feature of Scala, you don’t need them in many cases, and you have options like this to achieve almost the same without self-types.

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

Sidebar

Related Questions

scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
Why does this code crash the Scala 2.8.1 compiler? val a = new Array[{
Take a look at this: scala> class Container(val rows: Iterable[Iterable[Option[Any]]]) {} defined class Container
scala> class A defined class A scala> trait B defined trait B Creating an
I had a Scala class as follows: class ClassA(val name: String) { println(this is
I am trying to get this working in Scala: class MyClass(some: Int, func: AnyRef*
Scala programmer should have known that this sort of writing : class Person{ var
In Scala IDE I get the following error about the class I am to
Does Scala have a version of Rubys' each_slice from the Array class?
scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined

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.