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

Make val field in implicit class private #542

Closed
olafurpg opened this issue Jan 4, 2018 · 3 comments
Closed

Make val field in implicit class private #542

olafurpg opened this issue Jan 4, 2018 · 3 comments

Comments

@olafurpg
Copy link
Contributor

olafurpg commented Jan 4, 2018

I just observed that the val field in an implicit class is accesible as an extension method

scala> implicit class XtensionString(val str: String) extends AnyVal {
     |   def doubled = str + str
     | }
defined class XtensionString

scala> "message".str
res0: String = message

A workaround to prevent this is to make the field private

scala> implicit class XtensionString(private val str: String) extends AnyVal

It would be nice to have a scalafix rule to automatically fix this.

Context: https://gitter.im/scala/contributors?at=5a4e57685355812e57502b5b

@LeonardMeyer
Copy link
Contributor

LeonardMeyer commented Jan 6, 2018

I took a stab at this. Can't really test right now because of #547 but will do.

I do have one question though : Why does Ctor.Primary of ScalaMeta has a List[List[Term.Param]] ? Why two levels ?

@olafurpg
Copy link
Contributor Author

olafurpg commented Jan 6, 2018

Cool @LeonardMeyer ! I will try to take a look at #547 in the coming week.

Primary constructors can be curried which are represented as List[List[Term.Param]]

@ show(q"class A(a: Int)(b: String)".ctor)
Ctor.Primary(
  List(),
  _,
  List(
    List(Term.Param(List(), Term.Name("a"), Some(Type.Name("Int")), None)),
    List(Term.Param(List(), Term.Name("b"), Some(Type.Name("String")), None))
  )
)

@LeonardMeyer
Copy link
Contributor

LeonardMeyer commented Jan 6, 2018

I literally realized this right before coming back here 😄 By the way, even private won't help with something like this :

class A(val str: String) {
  def doubled: String = str + str
}

implicit class B(override val str: String) extends A(str)

"test".str  //visible

LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 7, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 8, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 13, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 13, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 13, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 17, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 17, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 18, 2018
LeonardMeyer added a commit to LeonardMeyer/scalafix that referenced this issue Jan 19, 2018
olafurpg added a commit that referenced this issue Jan 23, 2018
@MasseGuillaume MasseGuillaume added this to the v0.5.8 milestone Jan 25, 2018
xerial added a commit to wvlet/airframe that referenced this issue Jan 9, 2024
Testing scalafix.

Due to scala2/3 compatibilities, we cannot enable some common rules, but
at least scalacenter/scalafix#542 worked
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

3 participants