Scala: multiple objects and classes in a single file or each object/class its own file -
i've started programming in scala, coming python , java wondering right way or accepted way when defining objects/classes in scala. scala supports, python, add together several class or object definitions in single file.
so purely accepted construction perspective, every object need defined in own file or allowed take yourself?
there chapter in official scala style guide on this. it's pretty clear in itself, i'll leave quotes here.
the core thought is:
as rule, files should contain single logical compilation unit. “logical” mean class, trait or object.
there is, of course, exception companion objects:
one exception guideline classes or traits have companion objects. companion objects should grouped corresponding class or trait in same file.
there fact sealed works within same file.
despite said above, there of import situations warrant inclusion of multiple compilation units within single file. 1 mutual illustration of sealed trait , several sub-classes. because of nature of sealed superclasses (and traits), subtypes must included in same file.
most of time, case classes simple info containers , can grouped together.
another case when multiple classes logically form single, cohesive group, sharing concepts point maintenance served containing them within single file.
finally, there naming convention exempted multi-unit scala files:
all multi-unit files should given camelcase names lower-case first letter.
so: set scala classes , objects in separate files, unless fall 1 of 3 mentioned exceptions.
scala
No comments:
Post a Comment