Skip to content

Commit eb74f7d

Browse files
committed
Docs cleanups
- Fix outdated syntax - Switch to indentation syntax almost everywhere - Explain soft keywords in detail - Switch to 3 space indent The switch to 3 space is an experiment, we can undo it separately if we don;t like it.
1 parent 46290f9 commit eb74f7d

Some content is hidden

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

63 files changed

+1621
-1849
lines changed

docs/docs/internals/explicit-nulls.md

Lines changed: 3 additions & 4 deletions
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

docs/docs/internals/syntax.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ yield
104104
### Soft keywords
105105

106106
```
107-
as derives end extension inline opaque open transparent using
108-
* + -
107+
derives end extension inline infix opaque oapque open
108+
transparent using | * + -
109109
```
110-
111110
## Context-free Syntax
112111

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

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

Lines changed: 20 additions & 23 deletions
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"
65+
class DivideZero extends StandardPlugin:
66+
val name: String = "divideZero"
67+
override val description: String = "divide zero check"
6868

69-
def init(options: List[String]): List[PluginPhase] =
70-
(new DivideZeroPhase) :: Nil
71-
}
69+
def init(options: List[String]): List[PluginPhase] =
70+
(new DivideZeroPhase) :: Nil
7271

73-
class DivideZeroPhase extends PluginPhase {
74-
import tpd._
72+
class DivideZeroPhase extends PluginPhase:
73+
import tpd._
7574

76-
val phaseName = "divideZero"
75+
val phaseName = "divideZero"
7776

78-
override val runsAfter = Set(Pickler.name)
79-
override val runsBefore = Set(Staging.name)
77+
override val runsAfter = Set(Pickler.name)
78+
override val runsBefore = Set(Staging.name)
8079

81-
override def transformApply(tree: Apply)(implicit ctx: Context): Tree = {
82-
tree match {
80+
override def transformApply(tree: Apply)(implicit ctx: Context): Tree =
81+
tree match
8382
case Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0))))
8483
if rcvr.tpe <:< defn.IntType =>
8584
report.error("dividing by zero", tree.pos)
8685
case _ =>
8786
()
88-
}
89-
tree
90-
}
91-
}
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 7 deletions
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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ 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 = {
23-
implicit val ctx = this.ctx // ok
24-
...
25-
}
20+
/*!*/ implicit def y = ... // error: type must be given explicitly
21+
}
22+
val y = {
23+
implicit val ctx = this.ctx // ok
24+
...
25+
}
2626
```
2727
**2.** Nesting is now taken into account for selecting an implicit. Consider for instance the following scenario:
2828
```scala
@@ -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-
given a: A = A()
4544

46-
object o {
45+
given a: A = A()
46+
47+
object o:
4748
given b: B = B()
4849
type C
49-
}
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

Lines changed: 24 additions & 30 deletions
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

0 commit comments

Comments
 (0)