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

Rewrite SAM #154

Closed
gabro opened this issue May 8, 2017 · 8 comments
Closed

Rewrite SAM #154

gabro opened this issue May 8, 2017 · 8 comments
Assignees

Comments

@gabro
Copy link
Collaborator

gabro commented May 8, 2017

Not sure this can be fully automated, but since Scala 2.12 has support for SAM some rewrite like this should be possible:

Given

trait SomeSAM {
  def foo(x: Int)
}

Then this

val aSam = new SomeSAM {
  def foo(x: Int) = x
}

can become (in Scala 2.12)

val aSam: SomeSAM = (x: Int) => x
@gabro gabro changed the title Optimize SAM Rewrite SAM May 8, 2017
@olafurpg
Copy link
Contributor

olafurpg commented May 9, 2017

This is a cool idea! I think we will need Type.members to implement this rewrite because iiuc then the SAM type must have only one method?

val aSam = (x: Int) => x

Should that be val aSam: SomeSAM = (x: Int) => x?

@gabro
Copy link
Collaborator Author

gabro commented May 9, 2017

Correct, SAMs must have only one method (SAM stands for Single Abstract Method), and yes, I think the type annotation is required.

@gabro gabro added the rewrite label Jun 5, 2017
@gabro
Copy link
Collaborator Author

gabro commented Jun 9, 2017

So a plan for this could be:

  • look for instantiations of traits new SomeTrait { ... }
  • check in the semanticdb that SomeTrait defines a single abstract method (not sure how doable this is in case of trait inheritance)
  • replace the trait with a function (this is easy)

I'm not sure how many edge cases we're going to cover, but it should be ok, since we would probably not generating false positives. Simply, some patterns would go unrecognized and lead to empty patches. I think this is worth a shot.

@olafurpg olafurpg added this to the v0.5.9 milestone Dec 15, 2017
@MasseGuillaume
Copy link
Contributor

MasseGuillaume commented Jan 11, 2018

I'm starting to work on this. #552

The spec for the SAM conversion is defined here: http://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#sam-conversion

@olafurpg
Copy link
Contributor

This is a good idea. After the PR #827, we are moving most built-in rules outside of the scalacenter/scalafix repository so that they can evolve at a different pace than Scalafix APIs. As long as a custom rule meets a certain level of quality, it can still

  • be automatically be included by sbt-scalafix
  • have tab completion in the sbt shell like built-in rules
  • be documented on the main Scalafix website

Ping us on Gitter if you believe your rule is good enough for inclusion. Don't hesitate to ask for help around code review or general guidance on writing/sharing custom rules.

@joroKr21
Copy link

Does this rule exist somewhere now?

@bjaglin
Copy link
Collaborator

bjaglin commented Apr 27, 2021

Does this rule exist somewhere now?

Unfortunately not, to my knowledge.

@joroKr21
Copy link

Unfortunately not, to my knowledge.

Thanks, I would like to have it so if I come up with something useful I will post back here 👍

bjaglin pushed a commit to liancheng/scalafix that referenced this issue May 23, 2023
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

5 participants