scaladoc

A doc comment is the one kind of comment the compiler keeps. It is written /** ... */ before a definition, it may carry tags such as @param and @return, and scaladoc 3.10.1 reads it from the compiled .tasty files and writes a small site: one page per package, one entry per member, the parameters and the returned value in a table.

A documented object

The first comment describes the object, and the two below describe one method each. @param names a parameter exactly as the signature spells it and says what it must be, and the methods and parameters lesson is where a signature like it is declared; @return says what the caller gets. A tag that names a parameter the method does not have is a documentation warning:

Geometry.scala

package shapes /** Area and circumference of a circle. * * The object holds the only constant the formulas need. */ object Geometry: /** The area of a circle. * * @param radius the radius, a positive number * @return the area in the unit of the radius, squared */ def area(radius: Double): Double = math.Pi * radius * radius /** The circumference of a circle. * * @param radius the radius, a positive number * @return the length around the circle */ def circumference(radius: Double): Double = 2 * math.Pi * radius

mkdir -p out classes

scalac -color:never -d classes Geometry.scala

scaladoc -d out classes

find out -name '*.html' | sort

out/index.html
out/shapes.html
out/shapes/Geometry$.html

The compiler writes the .tasty file and scaladoc reads it, which is why the folder classes comes first: scaladoc does not parse source, so the documentation it writes is the documentation of the code that was compiled. A build puts those class files where the sbt project layout says, instead of a folder made by hand. The package name becomes the page shapes.html, and the object gets one page under shapes/.

What the page says

Each tag becomes a section of the entry. The text of @param radius sits under Value parameters, and the text of @return under Returns:

sed -n '/Value parameters/,+6p' 'out/shapes/Geometry$.html' | head -7

             <h2 class="h200">Value parameters</h2>
             <dl class="attributes attributes-small">
              <dt class="body-small">radius</dt>
              <dd class="body-medium">
               <p>the radius, a positive number</p>
              </dd>
             </dl>

A reader opens the page in a browser and gets the same text with a navigation tree, a search box and a link to the source. The value of the tags is not the page a person reads: it is that an editor shows @param and @return on hover, and that -Wunused and the doc warnings make a stale comment a build finding rather than a paragraph nobody noticed, and a trait a library exposes, such as a type class, is where those comments matter most.

Автор статьи: Arthur Isaev