Tuesday 24 July 2018

Scala Comments with Syntax and Examples

Scala Comments with Syntax and Examples

1. Scala Comments
The objective of this ‘Scala Comments’ article is to tell you about comments in Scala. Let’s begin.
Scala Comments
Scala Comments

2. Introduction to Scala Comments

Comments are entities, in your code, that the compiler/interpreter ignores. You can use them to explain code, and also to hide code details.

3. Scala Single-line Comments

When you want only one line of a comment, you can use the characters ‘//’ preceding the comment.
object Main extends App
  1. {
  2. print("Hi!")
  3. //This is a comment, and will not print
  4. }
Here’s the output:
Hi!

4. Scala Multiline Comments

When your comments will span more than one line, you can use a multiline comment. To declare it, we use the characters ‘/*’ and ‘*/’ around the comment.
object Main extends App
  1. {
  2. print("Hi!")
  3. /*This is a multiline comment,
  4. and will not print
  5. kbye*/
  6. }
The output:
Hi!

5. Documentation Comments

A documentation comment is available for quick documentation lookup, and are open for review on pressing Ctrl+Q in the IntelliJ. Your compiler uses these comments to document the source code.
We have the following syntax for creating a documentation comment:
object Main extends App
  1. {
  2. print("Hi!")
  3. /**
  4. * This is a documentation comment
  5. * This is a demo
  6. */
  7. }
Output:
Hi!
To declare such a comment, type the characters ‘/**’, and then type something, or press Enter. The IDE will put in a ‘*’ every time you press enter. To end such a comment, type ‘/’ after one of the carets(*).

6. Conclusion

Concluding Scala Comments article, Scala has three kinds of comments- single-line, multiline, and documentation comments.

0 comments:

Post a Comment