Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala style: More than one class in a file?

Unlike Java, Scala supports putting multiple classes in one file. Since Scala's classes are often quite short (think case classes), this often seems to make sense.

What is considered the proper style and idiom to do this, on production code? Under what circumstances should short or associated classes be put in the same file? How should that file be named?

like image 627
SRobertJames Avatar asked Mar 19 '23 02:03

SRobertJames


1 Answers

This is all covered in Scala style guide

Summary :

  • As a rule of thumb, you should have one logical compilation unit (i.e trait, class, object) per file.
  • Exception made of companion objects where you can have them as well as their original trait/class in the same file
  • Another exception is sealed trait with its subclasses
  • Last exception is if it makes way more sense maintenance wise (i.e your logical compilation units forms a inseparable, cohesive group)
  • Multi-unit file should have a lowercase first letter.
like image 183
LMeyer Avatar answered Mar 23 '23 05:03

LMeyer