Skip to content
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

Different behaviors between AnyVal/AnyRef case classes with private/protected constructors. #8491

Closed
scabug opened this issue Apr 10, 2014 · 2 comments

Comments

@scabug
Copy link

scabug commented Apr 10, 2014

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class PrivateConstructorVal private (val underlying: String) extends AnyVal
defined class PrivateConstructorVal

scala> case class ProtectedConstructorVal protected (val underlying: String) extends AnyVal
defined class ProtectedConstructorVal

scala> case class PrivateConstructorRef private (val underlying: String)
defined class PrivateConstructorRef

scala> case class ProtectedConstructorRef protected (val underlying: String)
defined class ProtectedConstructorRef

scala> PrivateConstructorVal("")
res0: PrivateConstructorVal = PrivateConstructorVal()

scala> ProtectedConstructorVal("")
<console>:10: error: constructor ProtectedConstructorVal in class ProtectedConstructorVal cannot be accessed in object $iw
 Access to protected constructor ProtectedConstructorVal not permitted because
 enclosing object $iw is not a subclass of 
 class ProtectedConstructorVal where target is defined
              ProtectedConstructorVal("")
              ^

scala> PrivateConstructorRef("")
res2: PrivateConstructorRef = PrivateConstructorRef()

scala> ProtectedConstructorRef("")
<console>:10: error: constructor ProtectedConstructorRef in class ProtectedConstructorRef cannot be accessed in object $iw
 Access to protected constructor ProtectedConstructorRef not permitted because
 enclosing object $iw is not a subclass of 
 class ProtectedConstructorRef where target is defined
              ProtectedConstructorRef("")
              ^

I don't know which behavior is correct, but I guess they could behave the same.

@scabug
Copy link
Author

scabug commented Apr 10, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8491?orig=1
Reporter: @Atry
Affected Versions: 2.10.3

@som-snytt
Copy link

That was then:

➜  snips cat t8491.scala

case class V private (s: String) extends AnyVal
case class W protected (s: String) extends AnyVal
case class X private (s: String)
case class Y protected (s: String)

object Test extends App {
  println {(
    V("v"),
    W("w"),
    X("x"),
    Y("y")
  )}
}
➜  snips scalac -d /tmp t8491.scala
t8491.scala:10: error: constructor W in class W cannot be accessed in object Test
 Access to protected constructor W not permitted because
 enclosing object Test is not a subclass of
 class W where target is defined
    W("w"),
    ^
t8491.scala:11: error: constructor X in class X cannot be accessed in object Test
    X("x"),
    ^
t8491.scala:12: error: constructor Y in class Y cannot be accessed in object Test
 Access to protected constructor Y not permitted because
 enclosing object Test is not a subclass of
 class Y where target is defined
    Y("y")
    ^
three errors found
➜  snips scalac -version
Scala compiler version 2.10.7 -- Copyright 2002-2017, LAMP/EPFL

This was also then:

➜  snips sdk use scala 2.11.12

Using scala version 2.11.12 in this shell.
➜  snips scalac -d /tmp t8491.scala
➜  snips

This is now:

➜  snips sdk use scala 3.1.1

Using scala version 3.1.1 in this shell.
➜  snips scalac -d /tmp t8491.scala
-- Error: t8491.scala:9:4 ----------------------------------------------------------------------------------------------
9 |    V("v"),
  |    ^
  |    method apply cannot be accessed as a member of V.type from module class Test$.
-- Error: t8491.scala:11:4 ---------------------------------------------------------------------------------------------
11 |    X("x"),
   |    ^
   |    method apply cannot be accessed as a member of X.type from module class Test$.
2 errors found
➜  snips

This is also now:

➜  snips sdk use scala 2.13.8

Using scala version 2.13.8 in this shell.
➜  snips scalac -Xsource:3 -d /tmp t8491.scala
t8491.scala:9: error: method apply in object V cannot be accessed as a member of object V from object Test
error after rewriting to V.<apply: error>
possible cause: maybe a wrong Dynamic method signature?
    V("v"),
    ^
t8491.scala:11: error: method apply in object X cannot be accessed as a member of object X from object Test
error after rewriting to X.<apply: error>
possible cause: maybe a wrong Dynamic method signature?
    X("x"),
    ^
2 errors

scala/scala#7702

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

No branches or pull requests

2 participants