Comments and Style
Prerequisites: Methods
Comments
// A single-line comment
val taxRate = 0.2 // after code
/* A block comment
spanning several lines */
/** A Scaladoc comment documents the definition below it.
*
* @param price the net price
* @return the price including tax
*/
def withTax(price: Double): Double = price * (1 + taxRate)
@main def comments(): Unit =
println(withTax(100))
120.0
Scaladoc comments (/** ... */) appear when you hover over a name in
your editor, and scaladoc turns them into an HTML API reference.
Style guidelines
The official Scala style guide recommends:
- camelCase for values and methods, PascalCase for classes, traits, objects, and enums.
- Two-space indentation.
valovervar, immutable collections over mutable ones.- Explicit return types on public methods.
- Short, expressive names in small scopes (
n,x), descriptive names in larger ones (activeUsers). - Methods without parameters that have no side effects are written without
parentheses (
list.size); methods with side effects keep them (reset()).
Tools
- scalafmt formats code automatically (
scala fmt .). - Scalafix and the compiler's warnings (
-Wunused:all) find unused imports and other problems.
What's next
You now know the core of Scala. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with classes and case classes.