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

Refactor to Single Abstract Method (SAM) #253

Open
benjamingeer opened this issue Feb 2, 2022 · 2 comments
Open

Refactor to Single Abstract Method (SAM) #253

benjamingeer opened this issue Feb 2, 2022 · 2 comments

Comments

@benjamingeer
Copy link

Is your feature request related to a problem? Please describe.

Lambda syntax for Single Abstract Method (SAM) types improves interoperability with Java libraries, and is more concise than the alternative syntax, but it's cumbersome to change from one to the other manually.

Describe the solution you'd like

Taking an example from Stackoverflow, given a trait like this:

trait Foo {
  def m1(id : Int) : Try[String]
}

And an expression like this:

val g : Foo = new Foo {
  override def m1(id: Int): Try[String] = Success("Good job")
}

I'd like Metals to be able to refactor the expression to produce this:

val g : Foo = (id: Int) => Success("Good job")

Describe alternatives you've considered

This can be done manually, but it's more time-consuming.

Additional contex

No response

Search terms

SAM Java refactor

@kubukoz
Copy link

kubukoz commented Nov 3, 2022

I'd just like to point out that this isn't a purely syntactic refactor unfortunately: if the type annotation isn't present, Scala can't tell what interface is necessary.

I would also love this feature though!

@tgodzik
Copy link
Contributor

tgodzik commented Nov 3, 2022

I think this is doable via the presentation compiler, the problem is detecting the places where it can be done. Would be great if the presentation compiler returned a warning where it can be converted.

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

3 participants