Extract trait in Scala
With the Extract Trait refactoring you can extract selected members from a Scala class, object, or a trait into a new trait. IntelliJ IDEA supports
var type members.
In the editor, select code members that you want to refactor.
- From the main menu, select
Alternatively, right-click anywhere in the editor to open the context menu and select. .
- In the dialog that opens, enter a new trait name and select members you want to extract.
If you need to select all of the listed members in the Members to extract section, press Ctrl+A first and thenSpace.Click Refactor.
Let's create a class
Calculator with several methods and extract one of the methods (
mul) into a trait.
IntelliJ IDEA creates a new trait
Multiplier that contains the
mul method with its definition body. The class
Calculator extends the trait
Extract trait dialog reference
|Extract trait from||This field contains the name of the class, object, or trait from which you are trying to extract a member.|
|Trait name||Use this field to enter a name for your new trait.|
|Package for trait||Use this field to add a package for the newly created trait. We recommend that you use the same package as the original class, object, or trait from which you are extracting.|
|Members to extract||Use this area to select members you want to extract. |
When the extract abstracts checkbox is selected then the new trait will only contain a declaration without a definition body. For example, an abstract method that doesn't contain a body in such case will be overridden in the original class, and the definition will be left there.
When this checkbox is not selected, the method will be moved to the new trait with its definition as in the example.