Skip to content

Commit bf0b9d1

Browse files
committed
scaladoc: use ellipsis to indicate optional parameters in signatures
currently, there is no indication in the scaladoc when parameters may be optional. this leads to [long and overwhelming signatures][1] which is not at all user-friendly. users are forced to first intuit that this method *might* have some optional parameters, then manually find [the source code][2] to learn which parameters are optional. [1]: https://javadoc.io/static/com.lihaoyi/os-lib_3/0.11.5/os/proc.html#call-fffff910 [2]: https://github.com/com-lihaoyi/os-lib/blob/0.11.5/os/src/ProcessOps.scala#L192-L205 this PR suffixes `= ...` after the type signature of optional parameters. this makes it possible to tell that these parameters are optional and may be omitted. this applies to both method parameters and class parameters. the format is intentionally similar to the definition of such optional parameters in code: ```scala // new scaladoc display: def f(x: Int, s: String = ...): Nothing // code: def f(x: Int, s: String = "a"): Nothing ``` of course, the `...` term is different. i think this is a reasonable choice because (1) ellipsis commonly represents something present but omitted, and (2) it is not valid Scala, so there is no risk someone will think this is denoting a literal default value of `...`. a proper ellipsis character (rather than 3 periods) could also be considered, but i found that looked out of place amongst the monospace signature. about displaying the default value itself, this PR does not display the default value. this is because of anticipated difficulties around displaying an expression. this could be re-visited in future, but i think it should not hold up this PR. i believe that this PR alone is already a substantial improvement for the documentation of optional parameters.
1 parent e0862a3 commit bf0b9d1

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

scaladoc-testcases/src/tests/extendsCall.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package extendsCall
33

44
class Impl() extends Base(Seq.empty, c = "-") //expected: class Impl() extends Base
55

6-
class Base(val a: Seq[String], val b: String = "", val c: String = "") //expected: class Base(val a: Seq[String], val b: String, val c: String)
6+
class Base(val a: Seq[String], val b: String = "", val c: String = "") //expected: class Base(val a: Seq[String], val b: String = ..., val c: String = ...)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tests
2+
package optionalParams
3+
4+
class C(val a: Seq[String], val b: String = "", var c: String = "") //expected: class C(val a: Seq[String], val b: String = ..., var c: String = ...)
5+
{
6+
def m(x: Int, s: String = "a"): Nothing //expected: def m(x: Int, s: String = ...): Nothing
7+
= ???
8+
}
9+
10+
def f(x: Int, s: String = "a"): Nothing //expected: def f(x: Int, s: String = ...): Nothing
11+
= ???
12+
13+
extension (y: Int)
14+
def ext(x: Int = 0): Int //expected: def ext(x: Int = ...): Int
15+
= 0
16+
17+
def byname(s: => String = "a"): Int //expected: def byname(s: => String = ...): Int
18+
= 0
19+
20+
enum E(val x: Int = 0) //expected: enum E(val x: Int = ...)
21+
{
22+
case E1(y: Int = 10) extends E(y) //expected: final case class E1(y: Int = ...) extends E
23+
}

scaladoc/src/dotty/tools/scaladoc/tasty/ClassLikeSupport.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,15 @@ trait ClassLikeSupport:
443443
val inlinePrefix = if symbol.flags.is(Flags.Inline) then "inline " else ""
444444
val name = symbol.normalizedName
445445
val nameIfNotSynthetic = Option.when(!symbol.flags.is(Flags.Synthetic))(name)
446+
val defaultValue = Option.when(symbol.flags.is(Flags.HasDefault))(Plain(" = ..."))
446447
api.TermParameter(
447448
symbol.getAnnotations(),
448449
inlinePrefix + prefix(symbol),
449450
nameIfNotSynthetic,
450451
symbol.dri,
451-
argument.tpt.asSignature(classDef, symbol.owner),
452-
isExtendedSymbol,
453-
isGrouped
452+
argument.tpt.asSignature(classDef, symbol.owner) :++ defaultValue,
453+
isExtendedSymbol = isExtendedSymbol,
454+
isGrouped = isGrouped
454455
)
455456

456457
def mkTypeArgument(

scaladoc/test/dotty/tools/scaladoc/signatures/TranslatableSignaturesTestCases.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,5 @@ class RightAssocExtension extends SignatureTest("rightAssocExtension", Signature
130130
class NamedTuples extends SignatureTest("namedTuples", SignatureTest.all)
131131

132132
class InnerClasses extends SignatureTest("innerClasses", SignatureTest.all)
133+
134+
class OptionalParams extends SignatureTest("optionalParams", SignatureTest.all)

0 commit comments

Comments
 (0)