question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The meaning of `this.field` in class body

See original GitHub issue

Compiler version

Scala 2 and Scala 3 (master)

Minimized code

class B(val y: Int):
  println(this.y)
  foo()
  def foo() = println(this.y)

class C(override val y: Int) extends B(10)

@main
def Test = new C(20)

Output

10
20

Expectation

I would expect this.y to have the same semantics both in the class body and method body.

It’s up to discussion whether an unqualified y in class body and in method body should have the same semantics.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
smartercommented, Jul 21, 2022

class C(override val y: Int) extends B(10)

Could we disallow overriding a class parameter unless the overridden val is passed to the supercall (as in B(y))?

3reactions
smartercommented, Jul 22, 2022

without fear of capture in C

That can be accomplised by writing @constructorOnly y: Int (https://github.com/lampepfl/dotty/blob/main/library/src/scala/annotation/constructorOnly.scala)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the this Keyword (The Java™ Tutorials > Learning the ...
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. For example,...
Read more >
What is the meaning of "this" in Java? - Stack Overflow
this refers to the current object. Each non-static method runs in the context of an object. So if you have a class like...
Read more >
Fields - C# Programming Guide | Microsoft Learn
A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing...
Read more >
Public class fields - JavaScript - MDN Web Docs - Mozilla
Public static fields are useful when you want a field to exist only once per class, not on every class instance you create....
Read more >
Classes are Object Factories - Interactive Programming In Java
This kind of information is stored in parts of the object called fields. A field is simply a name that is a part...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found