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

"Extract interface" code action to convert classes to abstract traits #355

Open
keynmol opened this issue Aug 22, 2023 · 0 comments
Open

Comments

@keynmol
Copy link

keynmol commented Aug 22, 2023

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

Not sure who else wants that, but I often find myself writing a class implementation and then realise that I'd rather have it as a trait + implementation class.

This code action can be especially useful on an already existing class with a non trivial amount of public methods implemented.

Describe the solution you'd like

A code action that

  1. Extracts all public methods into a trait named the same as the class
  2. Explicitly annotates the methods with inferred return types
  3. Renames class to NameImpl
  4. Converts all methods into overrides
object BEFORE:
  class Client(bla: String):
    def test() = 1
    def hello(x: String, b: Int) = bla + x

// Apply code action "Extract interface"

object AFTER:
  trait Client:
    def test(): Int
    def hello(x: String, b: Int): String

  class ClientImpl(bla: String) extends Client:
    override def test() = 1
    override def hello(x: String, b: Int) = bla + x

There's complexity around type parameters of course, but it doesn't have to be part of MVP.

Describe alternatives you've considered

For my personal use an alternative would be to plan ahead and always create traits, but I don't have that level of control over my life.

Additional context

No response

Search terms

code action, extract, trait, implementation, interface

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

1 participant