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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:17:47+00:00 2026-05-19T12:17:47+00:00

Working through these posts had me thinking I understood self-types, at least somewhat. So

  • 0

Working through these posts had me thinking I understood self-types, at least somewhat.

So I created an example which failed as expected:

scala> trait A { val v = "a" }
defined trait A

scala> trait B { this :A => ; var v = "" ; this.v = "b" }
<console>:6: error: reassignment to val
       trait B { this :A => ; var v = "" ; this.v = "b" }
                                                  ^

The self-type’s “this” shadows B’s “this” — it looks weird, but makes sense.

It would seem wise, then, to give the self-type a different name. I did this and was rather surprised:

scala> trait C { a :A => ; var v = "" ; this.v = "c" }
<console>:6: error: reassignment to val
       trait C { a :A => ; var v = "" ; this.v = "c" }
                                               ^

It’s still shadowed???

Changing the name of the ‘local’ variable let things compile, which makes sense:

scala> trait D { a :A => ; var w = "" ; this.w = a.v }
defined trait D

(And the self-type name can optionally be used to clarify which “v” to use.)

Okay. Which means that the following should fail?

scala> trait E { this :A => ; var w = "" ; this.w = this.v }
defined trait E

Huh? Which this is this? 🙁

So… is there a point to naming self-types? “this” seems to end up shadowed regardless.

Or is this an edge-case of scoping rules, where the self-type’s “this” takes precedence over the trait’s “this” — and one should just avoid using the same names for things in related traits?

  • 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-19T12:17:47+00:00Added an answer on May 19, 2026 at 12:17 pm

    Your problem is not the name of the self type (in all your examples both this and the alternate self-type name refer to the very same thing and have the same type, namely ‘the greatest lower bound of B and A’ [§5.1, Scala Language Spec]) but that you try to define a field with the same name again without explicitly overriding it.

    Look at the simpler example:

    trait A { val v = "a" }
    trait B { this: A =>
      var v = "b"
    }
    
    new A with B {} // does not compile
    
    <console>:9: error: overriding value v in trait A$class of type java.lang.String;
     variable v in trait B$class of type java.lang.String needs `override' modifier
       new A with B {}
    

    So, even though, you don’t get an error in defining B, you’re simply not able to use it anyway.

    This would work

    trait A { val v = "a" }
    trait B { this:A => override val v = "b"  }
    
    new A with B {}
    

    Here, you are explicitly overriding A’s v in B. (Note that new B with A {} would fail because B needs to come last.) Also, it has to be a val because in most cases you cannot really override vars and you cannot override something else using a var.

    Generally, you should not be concerned about the name of the self-type in these simple cases. As long as you do not create another trait or class inside B, both this and whatever you’d call your self-type variable will point to the same thing. There’ll be no shadowing. If you had a new trait inside B, and you needed to refer to the instance of B inside that trait, you’d need another name for your self-type.

    Consider this

    trait B { this =>
      val v = "b"
      trait C {
        val v = "c"
        println(this.v)
      }
      new C {}
    }
    new B {}
    
    // prints c
    

    vs this:

    trait B { self =>
      val v = "b"
      trait C {
        val v = "c"
        println(this.v)  // prints c
        println(self.v)  // prints b
      }
      new C {}
    }
    new B {}
    

    (Without any further type annotation, you could leave out all of the this in this example.)

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

Sidebar

Related Questions

No related questions found

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.