Comments in Scala
Scala comments are notes written inside source code for developers. The compiler ignores comment text, so comments do not become part of the generated bytecode or affect how the program runs.
Comments are commonly used to explain intent, document public classes and methods, record important constraints, or temporarily exclude code while investigating a problem.
Scala supports three useful forms of comments:
- single-line comments beginning with
// - multiline or block comments enclosed by
/*and*/ - Scaladoc comments enclosed by
/**and*/
Scala Single-Line Comments with //
Single line comments can be written in a scala program with two forward slashes before the text.
// this is a single line comment in scala
some scala code
// this is another single line comment in scala
Everything after // on the same line is treated as a comment. Code on the next line is processed normally.
object SingleLineCommentExample {
def main(args: Array[String]): Unit = {
// Store the amount before applying tax
val amount = 500
val tax = amount * 0.18 // Calculate 18 percent tax
println(amount + tax)
}
}
A single-line comment may appear on its own line or after a Scala statement. Comments placed above a statement are usually easier to read when the explanation is longer.
Scala Multiline Comments with /* and */
Multiline comments can be written in a scala program with the text being wrapped between /* and */.
/*this is an example
method
*/
A block comment can span several lines or appear within one line. It is useful when a note requires more space than a single-line comment.
object MultilineCommentExample {
def main(args: Array[String]): Unit = {
/*
* Convert the distance from kilometres to metres.
* One kilometre contains 1000 metres.
*/
val kilometres = 4
val metres = kilometres * 1000
println(metres)
}
}
The opening marker /* starts the comment, and the closing marker */ ends it. Any Scala code placed between these markers is ignored by the compiler.
Nested Block Comments in Scala
Scala supports nested block comments. This means one /* ... */ comment can appear inside another block comment, provided every opening marker has a matching closing marker.
object NestedCommentExample {
def main(args: Array[String]): Unit = {
/* Outer comment
/* Inner comment */
The outer comment continues here.
*/
println("Scala comments")
}
}
Nested comments can be useful when a large section of code already contains block comments. Even so, extensive commented-out code is usually better removed and recovered later from version control when needed.
Scaladoc Comments for Scala Classes and Methods
A Scaladoc comment begins with /** and ends with */. It is intended for API documentation rather than an ordinary implementation note.
Scaladoc comments are normally placed immediately before a class, trait, object, method, field, or other declaration that should appear in generated documentation.
/** Provides temperature conversion operations. */
object TemperatureConverter {
/**
* Converts Celsius to Fahrenheit.
*
* @param celsius temperature in degrees Celsius
* @return the equivalent temperature in degrees Fahrenheit
*/
def toFahrenheit(celsius: Double): Double = {
celsius * 9 / 5 + 32
}
}
Common Scaladoc tags include @param for parameters, @return for a returned value, @throws for exceptions, and @see for related APIs. Use only the tags that add useful information.
Difference Between Scala Comments and Scaladoc
| Comment type | Syntax | Main purpose |
|---|---|---|
| Single-line comment | // comment | Short notes about nearby code |
| Block comment | /* comment */ | Longer explanations or temporarily excluded code |
| Scaladoc comment | /** documentation */ | Generated documentation for APIs |
Ordinary comments explain implementation details to people reading the source. Scaladoc explains how a class, method, or field should be used by callers.
Commenting Out Scala Code Temporarily
To disable one line temporarily, place // before it. To disable several adjacent lines, wrap them in a block comment.
object CommentOutCodeExample {
def main(args: Array[String]): Unit = {
val first = 10
val second = 20
// println(first)
/*
println(second)
println(first + second)
*/
println("Program continues")
}
}
This technique is useful during a brief test, but commented-out code should not remain indefinitely. Source-control systems provide a clearer way to preserve older implementations.
Useful and Unhelpful Scala Comment Examples
A useful comment explains information that is not obvious from the code itself, such as a business rule, unusual workaround, unit assumption, or reason for a design decision.
// The external service accepts amounts in paise, not rupees.
val amountInPaise = amountInRupees * 100
A comment that only repeats the statement provides little value.
// Add one to count
val updatedCount = count + 1
In the second example, the code already states what happens. A more useful comment would explain why the count must be increased at that point.
Scala Comment Writing Guidelines
- Explain why, not only what: readable Scala code often makes the operation clear, while the reason may still require a comment.
- Keep comments current: update or remove a comment when the related code changes.
- Prefer clear names: improve unclear variable or method names instead of using comments to compensate for them.
- Use Scaladoc for public APIs: describe parameters, return values, side effects, and important constraints for callers.
- Avoid large blocks of disabled code: retain historical code in version control instead.
- Do not include sensitive information: passwords, access tokens, private URLs, and personal data should never appear in source comments.
Common Errors with Scala Comment Syntax
- Missing the closing marker: a block comment that begins with
/*must end with*/. - Using a single slash: a single-line comment requires two slashes,
//. - Placing code after
//unintentionally: everything after the marker on that line is ignored. - Using
/**for ordinary notes: reserve Scaladoc comments for declarations that need generated API documentation. - Leaving stale explanations: an incorrect comment can be more misleading than no comment.
Scala Comments FAQs
How do you write a single-line comment in Scala?
Begin the comment with //. All text from the two slashes to the end of that line is ignored by the compiler.
How do you write a multiline comment in Scala?
Place the text between /* and */. The comment may span one line or several lines.
Can Scala block comments be nested?
Yes. Scala permits a block comment inside another block comment, as long as the opening and closing markers are correctly paired.
What is the difference between /* and /** in Scala?
/* starts an ordinary block comment. /** starts a Scaladoc comment intended to document a declaration and appear in generated API documentation.
Do Scala comments affect program execution?
No. The Scala compiler ignores comments, so they do not execute and do not change the program result.
Scala Comments Editorial QA Checklist
- Confirm that every single-line example uses
//. - Confirm that every block comment has matching
/*and*/markers. - Confirm that Scaladoc examples begin with
/**and are placed before a declaration. - Check that comments explain intent, constraints, or usage rather than merely repeating the Scala statement.
- Verify that no example comment contains credentials, personal data, or outdated implementation details.
Scala Comments Tutorial Summary
Scala uses // for single-line comments, /* ... */ for multiline block comments, and /** ... */ for Scaladoc documentation. Use ordinary comments to explain implementation decisions and Scaladoc to describe APIs. Keep every comment accurate, specific, and useful to the next person reading the code.
TutorialKart.com