|
| 1 | +--- |
| 2 | +title: API Documentation |
| 3 | +--- |
| 4 | + |
| 5 | +# {{ page.title }} |
| 6 | + |
| 7 | +Scala3doc's main feature is creating API documentation from code comments. |
| 8 | + |
| 9 | +By default, the code comments are understood as Markdown, though we also support |
| 10 | +Scaladoc's old [Wiki syntax](https://docs.scala-lang.org/style/scaladoc.html). |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +### Definition links |
| 15 | + |
| 16 | +Our definition link syntax is quite close to Scaladoc's syntax, though we have made some |
| 17 | +quality-of-life improvements. |
| 18 | + |
| 19 | +#### Basic syntax |
| 20 | + |
| 21 | +A definition link looks as follows: `[[scala.collection.immutable.List]]`. |
| 22 | + |
| 23 | +Which is to say, a definition link is a sequence of identifiers separated by |
| 24 | +`.`. The identifiers can be separated with `#` as well for Scaladoc compatibility. |
| 25 | + |
| 26 | +By default, an identifier `id` references the first (in source order) entity |
| 27 | +named `id`. An identifier can end with `$`, which forces it to refer to a value |
| 28 | +(an object, a value, a given); an identifier can also end with `!`, which forces |
| 29 | +it to refer to a type (a class, a type alias, a type member). |
| 30 | + |
| 31 | +The links are resolved relative to the current location in source. That is, when |
| 32 | +documenting a class, the links are relative to the entity enclosing the class (a |
| 33 | +package, a class, an object); the same applies to documenting definitions. |
| 34 | + |
| 35 | +Special characters in links can be backslash-escaped, which makes them part of |
| 36 | +identifiers instead. For example, `` [[scala.collection.immutable\.List]] `` |
| 37 | +references the class named `` `immutable.List` `` in package `scala.collection`. |
| 38 | + |
| 39 | +#### New syntax |
| 40 | + |
| 41 | +We have extended Scaladoc definition links to make them a bit more pleasant to |
| 42 | +write and read in source. The aim was also to bring the link and Scala syntax |
| 43 | +closer together. The new features are: |
| 44 | + |
| 45 | +1. `package` can be used as a prefix to reference the enclosing package |
| 46 | + Example: |
| 47 | + ``` |
| 48 | + package utils |
| 49 | + class C { |
| 50 | + def foo = "foo". |
| 51 | + } |
| 52 | + /** See also [[package.C]]. */ |
| 53 | + class D { |
| 54 | + def bar = "bar". |
| 55 | + } |
| 56 | + ``` |
| 57 | + The `package` keyword helps make links to the enclosing package shorter |
| 58 | + and a bit more resistant to name refactorings. |
| 59 | +1. `this` can be used as a prefix to reference the enclosing classlike |
| 60 | + Example: |
| 61 | + ``` |
| 62 | + class C { |
| 63 | + def foo = "foo" |
| 64 | + /** This is not [[this.foo]], this is bar. */ |
| 65 | + def bar = "bar" |
| 66 | + } |
| 67 | + ``` |
| 68 | + Using a Scala keyword here helps make the links more familiar, as well as |
| 69 | + helps the links survive class name changes. |
| 70 | +1. Backticks can be used to escape identifiers |
| 71 | + Example: |
| 72 | + ``` |
| 73 | + def `([.abusive.])` = ??? |
| 74 | + /** TODO: Figure out what [[`([.abusive.])`]] is. */ |
| 75 | + def foo = `([.abusive.])` |
| 76 | + ``` |
| 77 | + Scaladoc required backslash-escaping to reference such identifiers. Instead, |
| 78 | + Scala3doc allows using the familiar Scala backtick quotation. |
| 79 | +
|
| 80 | +#### Why keep the Wiki syntax for links? |
| 81 | +
|
| 82 | +There are a few reasons why we've kept the Wiki syntax for documentation links |
| 83 | +instead of reusing the Markdown syntax. Those are: |
| 84 | +
|
| 85 | +1. Nameless links in Markdown are ugly: `[](definition)` vs `[[definition]]` |
| 86 | + By far, most links in documentation are nameless. It should be obvious how to |
| 87 | + write them. |
| 88 | +2. Local member lookup collides with URL fragments: `[](#field)` vs `[[#field]]` |
| 89 | +3. Overload resolution collides with MD syntax: `[](meth(Int))` vs `[[meth(Int)]]` |
| 90 | +4. Now that we have a parser for the link syntax, we can allow spaces inside (in |
| 91 | + Scaladoc one needed to slash-escape those), but that doesn't get recognized |
| 92 | + as a link in Markdown: `[](meth(Int, Float))` vs `[[meth(Int, Float)]]` |
| 93 | +
|
| 94 | +None of these make it completely impossible to use the standard Markdown link |
| 95 | +syntax, but they make it much more awkward and ugly than it needs to be. On top |
| 96 | +of that, Markdown link syntax doesn't even save any characters. |
0 commit comments