Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way New->Scala Class in intellij can default to creating a case class (vs a regular class)?

When I right-click and follow New->Scala Class, Intellij defaults to creating a new regular class. I'd like it to default to creating a case class.

A small thing... but annoying when creating a lot of new classes.

Is it configurable somewhere?

like image 988
Greg Avatar asked May 15 '19 21:05

Greg


People also ask

How do I create a case class in Scala?

Scala case classes are just regular classes which are immutable by default and decomposable through pattern matching. It uses equal method to compare instance structurally. It does not use new keyword to instantiate object. All the parameters listed in the case class are public and immutable by default.

How do I create a Scala class in Intellij?

On the Project pane on the left, right-click src and select New => Scala class. If you don't see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually.

Why does creating an instance of case class not require a new keyword?

Defining a case class Notice how the keyword new was not used to instantiate the Book case class. This is because case classes have an apply method by default which takes care of object construction. When you create a case class with parameters, the parameters are public val s. You can't reassign message1.

What is the difference between class and case class in Scala?

A class can extend another class, whereas a case class can not extend another case class (because it would not be possible to correctly implement their equality).


1 Answers

  1. Open File>Settings and go to Files and Code Templates.
  2. Click on + and add name Scala Case Class, set extension to scala and provide correct template. You might copy template from Scala Class and modify it accordingly. enter image description here
  3. Click on New > Scala Class. Dialog should have now option Scala Case Class.
like image 156
Krzysztof Atłasik Avatar answered Nov 15 '22 05:11

Krzysztof Atłasik