Skip to content

Commit 854f583

Browse files
authored
Merge pull request #10878 from dotty-staging/fix-docs
Docs cleanups
2 parents 4b9bf18 + d313f17 commit 854f583

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1720
-1949
lines changed

docs/docs/internals/explicit-nulls.md

+17-23
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ Within `Types.scala`, we defined some extractors to work with nullable unions:
7676
`OrNull` and `OrUncheckedNull`.
7777

7878
```scala
79-
(tp: Type) match {
80-
case OrNull(tp1) => // if tp is a nullable union: tp1 | Null
81-
case _ => // otherwise
82-
}
79+
(tp: Type) match
80+
case OrNull(tp1) => // if tp is a nullable union: tp1 | Null
81+
case _ => // otherwise
8382
```
8483

8584
This extractor will call utility methods in `NullOpsDecorator.scala`. All of these
@@ -112,30 +111,25 @@ The reason for casting to `x.type & T`, as opposed to just `T`, is that it allow
112111
support flow typing for paths of length greater than one.
113112

114113
```scala
115-
abstract class Node {
116-
val x: String
117-
val next: Node | Null
118-
}
119-
120-
def f = {
121-
val l: Node|Null = ???
122-
if (l != null && l.next != null) {
123-
val third: l.next.next.type = l.next.next
124-
}
125-
}
114+
abstract class Node:
115+
val x: String
116+
val next: Node | Null
117+
118+
def f =
119+
val l: Node|Null = ???
120+
if l != null && l.next != null then
121+
val third: l.next.next.type = l.next.next
126122
```
127123

128124
After typing, `f` becomes:
129125

130126
```scala
131-
def f = {
132-
val l: Node|Null = ???
133-
if (l != null && l.$asInstanceOf$[l.type & Node].next != null) {
134-
val third:
135-
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next.type =
136-
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next
137-
}
138-
}
127+
def f =
128+
val l: Node|Null = ???
129+
if l != null && l.$asInstanceOf$[l.type & Node].next != null then
130+
val third:
131+
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next.type =
132+
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next
139133
```
140134
Notice that in the example above `(l.type & Node).next.type & Node` is still a stable path, so
141135
we can use it in the type and track it for flow typing.

docs/docs/internals/syntax.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ yield
104104
### Soft keywords
105105

106106
```
107-
as derives end extension inline opaque open transparent using
108-
* + -
107+
derives end extension inline infix opaque open transparent using | * + -
109108
```
110-
111109
## Context-free Syntax
112110

113111
The context-free syntax of Scala is given by the following EBNF

docs/docs/reference/changed-features/compiler-plugins.md

+30-33
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,30 @@ import dotty.tools.dotc.core.Symbols._
6262
import dotty.tools.dotc.plugins.{PluginPhase, StandardPlugin}
6363
import dotty.tools.dotc.transform.{Pickler, Staging}
6464

65-
class DivideZero extends StandardPlugin {
66-
val name: String = "divideZero"
67-
override val description: String = "divide zero check"
68-
69-
def init(options: List[String]): List[PluginPhase] =
70-
(new DivideZeroPhase) :: Nil
71-
}
72-
73-
class DivideZeroPhase extends PluginPhase {
74-
import tpd._
75-
76-
val phaseName = "divideZero"
77-
78-
override val runsAfter = Set(Pickler.name)
79-
override val runsBefore = Set(Staging.name)
80-
81-
override def transformApply(tree: Apply)(implicit ctx: Context): Tree = {
82-
tree match {
83-
case Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0))))
84-
if rcvr.tpe <:< defn.IntType =>
85-
report.error("dividing by zero", tree.pos)
86-
case _ =>
87-
()
88-
}
89-
tree
90-
}
91-
}
65+
class DivideZero extends StandardPlugin:
66+
val name: String = "divideZero"
67+
override val description: String = "divide zero check"
68+
69+
def init(options: List[String]): List[PluginPhase] =
70+
(new DivideZeroPhase) :: Nil
71+
72+
class DivideZeroPhase extends PluginPhase:
73+
import tpd._
74+
75+
val phaseName = "divideZero"
76+
77+
override val runsAfter = Set(Pickler.name)
78+
override val runsBefore = Set(Staging.name)
79+
80+
override def transformApply(tree: Apply)(implicit ctx: Context): Tree =
81+
tree match
82+
case Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0))))
83+
if rcvr.tpe <:< defn.IntType =>
84+
report.error("dividing by zero", tree.pos)
85+
case _ =>
86+
()
87+
tree
88+
end DivideZeroPhase
9289
```
9390

9491
The plugin main class (`DivideZero`) must extend the trait `StandardPlugin`
@@ -111,13 +108,13 @@ import dotty.tools.dotc.core.Contexts.Context
111108
import dotty.tools.dotc.core.Phases.Phase
112109
import dotty.tools.dotc.plugins.ResearchPlugin
113110

114-
class DummyResearchPlugin extends ResearchPlugin {
115-
val name: String = "dummy"
116-
override val description: String = "dummy research plugin"
111+
class DummyResearchPlugin extends ResearchPlugin:
112+
val name: String = "dummy"
113+
override val description: String = "dummy research plugin"
117114

118-
def init(options: List[String], phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] =
119-
phases
120-
}
115+
def init(options: List[String], phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] =
116+
phases
117+
end DummyResearchPlugin
121118
```
122119

123120
A research plugin must extend the trait `ResearchPlugin` and implement the

docs/docs/reference/changed-features/implicit-conversions-spec.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The standard library defines an abstract class `Conversion`:
1717
package scala
1818
@java.lang.FunctionalInterface
1919
abstract class Conversion[-T, +U] extends Function1[T, U]:
20-
def apply(x: T): U
20+
def apply(x: T): U
2121
```
2222

2323
Function literals are automatically converted to `Conversion` values.

docs/docs/reference/changed-features/implicit-conversions.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ method that expects a `java.lang.Integer`
3434
```scala
3535
import scala.language.implicitConversions
3636
implicit def int2Integer(x: Int): java.lang.Integer =
37-
x.asInstanceOf[java.lang.Integer]
37+
x.asInstanceOf[java.lang.Integer]
3838
```
3939

4040
The second example shows how to use `Conversion` to define an
@@ -44,12 +44,11 @@ types:
4444
```scala
4545
import scala.language.implicitConversions
4646
implicit def ordT[T, S](
47-
implicit conv: Conversion[T, S],
48-
ordS: Ordering[S]
49-
): Ordering[T] = {
50-
// `ordS` compares values of type `S`, but we can convert from `T` to `S`
51-
(x: T, y: T) => ordS.compare(x, y)
52-
}
47+
implicit conv: Conversion[T, S],
48+
ordS: Ordering[S]
49+
): Ordering[T] =
50+
// `ordS` compares values of type `S`, but we can convert from `T` to `S`
51+
(x: T, y: T) => ordS.compare(x, y)
5352

5453
class A(val x: Int) // The type for which we want an `Ordering`
5554

docs/docs/reference/changed-features/implicit-resolution.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ affect implicits on the language level.
1111
must be explicitly declared. Excepted are only values in local blocks
1212
where the type may still be inferred:
1313
```scala
14-
class C {
14+
class C {
1515

16-
val ctx: Context = ... // ok
16+
val ctx: Context = ... // ok
1717

18-
/*!*/ implicit val x = ... // error: type must be given explicitly
18+
/*!*/ implicit val x = ... // error: type must be given explicitly
1919

20-
/*!*/ implicit def y = ... // error: type must be given explicitly
21-
22-
val y = {
20+
/*!*/ implicit def y = ... // error: type must be given explicitly
21+
}
22+
val y = {
2323
implicit val ctx = this.ctx // ok
2424
...
25-
}
25+
}
2626
```
2727
**2.** Nesting is now taken into account for selecting an implicit. Consider for instance the following scenario:
2828
```scala
2929
def f(implicit i: C) = {
30-
def g(implicit j: C) = {
31-
implicitly[C]
32-
}
30+
def g(implicit j: C) = {
31+
implicitly[C]
32+
}
3333
}
3434
```
3535
This will now resolve the `implicitly` call to `j`, because `j` is nested
@@ -41,12 +41,12 @@ no longer applies.
4141
**3.** Package prefixes no longer contribute to the implicit search scope of a type. Example:
4242
```scala
4343
package p
44+
4445
given a: A = A()
4546

46-
object o {
47-
given b: B = B()
48-
type C
49-
}
47+
object o:
48+
given b: B = B()
49+
type C
5050
```
5151
Both `a` and `b` are visible as implicits at the point of the definition
5252
of `type C`. However, a reference to `p.o.C` outside of package `p` will

docs/docs/reference/changed-features/main-functions.md

+24-30
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ Scala 3 offers a new way to define programs that can be invoked from the command
77
A `@main` annotation on a method turns this method into an executable program.
88
Example:
99
```scala
10-
@main def happyBirthday(age: Int, name: String, others: String*) = {
11-
val suffix =
12-
(age % 100) match {
10+
@main def happyBirthday(age: Int, name: String, others: String*) =
11+
val suffix =
12+
age % 100 match
1313
case 11 | 12 | 13 => "th"
1414
case _ =>
15-
(age % 10) match {
16-
case 1 => "st"
17-
case 2 => "nd"
18-
case 3 => "rd"
19-
case _ => "th"
20-
}
21-
}
22-
val bldr = new StringBuilder(s"Happy $age$suffix birthday, $name")
23-
for other <- others do bldr.append(" and ").append(other)
24-
bldr.toString
25-
}
15+
age % 10 match
16+
case 1 => "st"
17+
case 2 => "nd"
18+
case 3 => "rd"
19+
case _ => "th"
20+
val bldr = new StringBuilder(s"Happy $age$suffix birthday, $name")
21+
for other <- others do bldr.append(" and ").append(other)
22+
bldr.toString
2623
```
2724
This would generate a main program `happyBirthday` that could be called like this
2825
```
@@ -59,29 +56,26 @@ The Scala compiler generates a program from a `@main` method `f` as follows:
5956

6057
For instance, the `happyBirthDay` method above would generate additional code equivalent to the following class:
6158
```scala
62-
final class happyBirthday {
63-
import scala.util.{CommandLineParser => CLP}
64-
<static> def main(args: Array[String]): Unit =
65-
try
66-
happyBirthday(
67-
CLP.parseArgument[Int](args, 0),
68-
CLP.parseArgument[String](args, 1),
69-
CLP.parseRemainingArguments[String](args, 2))
70-
catch {
71-
case error: CLP.ParseError => CLP.showError(error)
72-
}
73-
}
59+
final class happyBirthday:
60+
import scala.util.{CommandLineParser => CLP}
61+
<static> def main(args: Array[String]): Unit =
62+
try
63+
happyBirthday(
64+
CLP.parseArgument[Int](args, 0),
65+
CLP.parseArgument[String](args, 1),
66+
CLP.parseRemainingArguments[String](args, 2))
67+
catch
68+
case error: CLP.ParseError => CLP.showError(error)
7469
```
7570
**Note**: The `<static>` modifier above expresses that the `main` method is generated
7671
as a static method of class `happyBirthDay`. It is not available for user programs in Scala. Regular "static" members are generated in Scala using objects instead.
7772

7873
`@main` methods are the recommended scheme to generate programs that can be invoked from the command line in Scala 3. They replace the previous scheme to write program as objects with a special `App` parent class. In Scala 2, `happyBirthday` could be written also like this:
7974

8075
```scala
81-
object happyBirthday extends App {
82-
// needs by-hand parsing of arguments vector
83-
...
84-
}
76+
object happyBirthday extends App:
77+
// needs by-hand parsing of arguments vector
78+
...
8579
```
8680

8781
The previous functionality of `App`, which relied on the "magic" `DelayedInit` trait, is no longer available. `App` still exists in limited form for now, but it does not support command line arguments and will be deprecated in the future. If programs need to cross-build

docs/docs/reference/changed-features/match-syntax.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,31 @@ The syntactical precedence of match expressions has been changed.
1010

1111
```scala
1212
xs match {
13-
case Nil => "empty"
14-
case x :: xs1 => "nonempty"
13+
case Nil => "empty"
14+
case x :: xs1 => "nonempty"
1515
} match {
16-
case "empty" => 0
17-
case "nonempty" => 1
16+
case "empty" => 0
17+
case "nonempty" => 1
1818
}
1919
```
2020

21+
(or, dropping the optional braces)
22+
23+
```scala
24+
xs match
25+
case Nil => "empty"
26+
case x :: xs1 => "nonempty"
27+
match
28+
case "empty" => 0
29+
case "nonempty" => 1
30+
```
31+
2132
2. `match` may follow a period:
2233

2334
```scala
24-
if xs.match {
25-
case Nil => false
26-
case _ => true
27-
}
35+
if xs.match
36+
case Nil => false
37+
case _ => true
2838
then "nonempty"
2939
else "empty"
3040
```

0 commit comments

Comments
 (0)