The meaning of `this.field` in class body
See original GitHub issueCompiler 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:
- Created a year ago
- Comments:12 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Could we disallow overriding a class parameter unless the overridden val is passed to the supercall (as in
B(y)
)?That can be accomplised by writing
@constructorOnly y: Int
(https://github.com/lampepfl/dotty/blob/main/library/src/scala/annotation/constructorOnly.scala)