-
Notifications
You must be signed in to change notification settings - Fork 21
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
Final, Anonymous, or singleton (objects) classes should not have protected non-overridden members #11434
Comments
There are reasonable use cases in Scala.js for Please don't make this idiom invalid. |
Then why not make them trait A { protected def x: Int = 0 }
trait B extends A { override def x: Int = 0} // x is now public Perhaps It could help to better understand the use cases you are referring to, if there was a small example of those, and why they would need |
Sometimes I use case class Foo(data: Map[String, Long])
object Foo {
protected implicit val encodeData: Encoder[Map[String, Long]] = ...
implicit val encoder: Encoder[Foo] = deriveEncoder // -> a macro
} If we make |
@diesalbla Here is a typical usecase: final class C {
// For Scala users, give a List[Int]
def xs: List[Int] = ???
// For JS callers, give a js.Array[Int]
@JSExport("xs")
protected def jsXs: js.Array[Int] = xs.toJSArray
} |
Scala is full of silly things you can do which one might think they want to be a compiler error. However, we also have scalafix, which can be made to emit that exact error. Moreover, while I would never have guessed that there would be a reason to use So I firmly think this should not be implemented. |
@joroKr21 Yeah, I have been bitten by such errors. I would say, though, that it is the linter that should be adjusted to the language, or disabled, not the other way around.
@sjrd Saw the use case. So, you use a modifier keyword with one meaning (to share with inheritors only) for a very different one (share with anyone in JavaScript). I do appreciate that porting Scala to another platform is difficult, but one may have wished for a cleaner way. En fin... |
Note: This is not a bug, it's a feature (request)
Consider the following code snippet, which compiles under Scala 2.12:
The
protected
access modifier lies between public andprivate
: it grants access to the member for the subclasses, but denies it to any other class. However, since final classes, cannot be extended, and have no sub-classes, it makes no sense for them to declare aprotected
member. Unless, of course, the said protected member was declared in a superclass, and so its access cannot be restricted toprivate
.Nevertheless, one can still desire for the compiler enforce the condition that a final class does not declare non-inherited protected members, either by:
protected
methods also be marked asoverridden
, in which case the compiler would detect if they are not declared in a super-type; orNote that here, super-class also includes traits or classes, and final classes includes anonymous classes and singleton classes.
The text was updated successfully, but these errors were encountered: