Skip to content

Self Type shadows constructor arguments. #3861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scabug opened this issue Sep 22, 2010 · 4 comments
Closed

Self Type shadows constructor arguments. #3861

scabug opened this issue Sep 22, 2010 · 4 comments
Assignees

Comments

@scabug
Copy link

scabug commented Sep 22, 2010

It seems that the self-type annotation shadows constructor arguments.

$$ scala
Welcome to Scala version 2.8.1.RC1 (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait Y
defined trait Y

scala> abstract class X(x: Int) { self: Y => x }
<console>:6: error: not found: value x
       abstract class X(x: Int) { self: Y => x }
                                             ^

Of course, without self-type annotation, there is no problem,

scala> abstract class X(x: Int) { self => x }
defined class X

and curiously there is no problem with 'val',

scala> abstract class X(val x: Int) { self: Y => x }
defined class X

This problem, for example, will cause the following unexpected result,

scala> object X {
     |   val x: Int = 1
     |   trait Y
     |   abstract class XX(x: Int){ self: Y =>
     |     println(x)
     |   }
     |   new XX(2) with Y
     | }
defined module X
scala> X
1
res0: X.type = X$$@1df3082
@scabug
Copy link
Author

scabug commented Sep 22, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3861?orig=1
Reporter: Atsuhiko Yamanaka (ymnk)

@scabug
Copy link
Author

scabug commented Sep 22, 2010

@paulp said:
For a related issue involving constructor parameter visibility, see #3194.

And your ticket made me realize that this bug represents a workaround to that one!

scala> class A(var x: Int)                                                      
defined class A

scala> class B(x: Int) extends A(x) { def f(z: Int) = this.x = z }              
<console>:6: error: reassignment to val
       class B(x: Int) extends A(x) { def f(z: Int) = this.x = z }
                                                             ^

scala> class B(x: Int) extends A(x) { self: A => def f(z: Int) = this.x = z }
defined class B

scala> new B(10)
res0: B = B@32e6e42e

scala> res0.x
res1: Int = 10

scala> res0.f(-75)

scala> res0.x
res3: Int = -75

Applying one bug to work around another: that's how we know we're engineers.

@scabug
Copy link
Author

scabug commented Feb 18, 2011

@dcsobral said:
I don't see the problems on trunk. I guess it was fixed?

scala> trait Y
defined trait Y

scala> abstract class X(x: Int) { self: Y => x }
defined class X

scala> scala> object X {
     |   val x: Int = 1
     |   trait Y
     |   abstract class XX(x: Int){ self: Y =>
     |     println(x)
     |   }
     |   new XX(2) with Y
     | }
defined module X
scala> X
1
res0: X.type = X$$@1df3082

Replaying 2 commands from interpreter transcript.
defined module X
2
res3: X.type = X$$@1e1370ab

@scabug
Copy link
Author

scabug commented Feb 18, 2011

@paulp said:
(In r24303) Test case closes #3861, no review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants