Skip to content

Commit 141bf9e

Browse files
authored
Merge pull request #10953 from michelou/scala3-docs
[docs/reference] more fixes in Markdown files
2 parents c149eeb + d96b2a2 commit 141bf9e

Some content is hidden

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

44 files changed

+242
-91
lines changed

docs/docs/reference/changed-features/eta-expansion-spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ The method value syntax `m _` is deprecated.
7373

7474
## Reference
7575

76-
For more info, see [PR #2701](https://github.com/lampepfl/dotty/pull/2701).
76+
For more information, see [PR #2701](https://github.com/lampepfl/dotty/pull/2701).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ Resolution](implicit-resolution.md) for more information.
119119
For more information about implicit resolution, see [Changes in
120120
Implicit Resolution](implicit-resolution.md).
121121
Other details are available in
122-
[PR #2065](https://github.com/lampepfl/dotty/pull/2065)
122+
[PR #2065](https://github.com/lampepfl/dotty/pull/2065).

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ title: "Main Methods"
66
Scala 3 offers a new way to define programs that can be invoked from the command line:
77
A `@main` annotation on a method turns this method into an executable program.
88
Example:
9+
910
```scala
1011
@main def happyBirthday(age: Int, name: String, others: String*) =
1112
val suffix =
@@ -21,11 +22,14 @@ Example:
2122
for other <- others do bldr.append(" and ").append(other)
2223
bldr.toString
2324
```
25+
2426
This would generate a main program `happyBirthday` that could be called like this
27+
2528
```
2629
> scala happyBirthday 23 Lisa Peter
2730
Happy 23rd Birthday, Lisa and Peter!
2831
```
32+
2933
A `@main` annotated method can be written either at the top-level or in a statically accessible object. The name of the program is in each case the name of the method, without any object prefixes. The `@main` method can have an arbitrary number of parameters.
3034
For each parameter type there must be an instance of the `scala.util.FromString` type class
3135
that is used to convert an argument string to the required parameter type.
@@ -52,9 +56,10 @@ The Scala compiler generates a program from a `@main` method `f` as follows:
5256
- The class has a static method `main` with the usual signature. It takes an `Array[String]`
5357
as argument and returns `Unit`.
5458
- The generated `main` method calls method `f` with arguments converted using
55-
methods in the `scala.util.CommandLineParser` object.
59+
methods in the [`scala.util.CommandLineParser` object](https://dotty.epfl.ch/api/scala/util/CommandLineParser$.html).
5660

5761
For instance, the `happyBirthDay` method above would generate additional code equivalent to the following class:
62+
5863
```scala
5964
final class happyBirthday:
6065
import scala.util.{CommandLineParser => CLP}
@@ -67,6 +72,7 @@ final class happyBirthday:
6772
catch
6873
case error: CLP.ParseError => CLP.showError(error)
6974
```
75+
7076
**Note**: The `<static>` modifier above expresses that the `main` method is generated
7177
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.
7278

@@ -78,5 +84,5 @@ object happyBirthday extends App:
7884
...
7985
```
8086

81-
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
87+
The previous functionality of `App`, which relied on the "magic" [`DelayedInit`](../dropped-features/delayed-init.md) 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
8288
between Scala 2 and Scala 3, it is recommended to use an explicit `main` method with an `Array[String]` argument instead.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: doc-page
3-
title: Match Expressions
3+
title: "Match Expressions"
44
---
55

66
The syntactical precedence of match expressions has been changed.

docs/docs/reference/changed-features/numeric-literals.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
---
22
layout: doc-page
3-
title: Numeric Literals
3+
title: "Numeric Literals"
44
---
55

66
**Note**: This feature is not yet part of the Scala 3 language definition. It can be made available by a language import:
7+
78
```scala
89
import scala.language.experimental.genericNumberLiterals
910
```
1011

11-
In Scala 2, numeric literals were confined to the primitive numeric types `Int`, `Long`, `Float`, and `Double`. Scala 3 allows to write numeric literals also for user defined types. Example:
12+
In Scala 2, numeric literals were confined to the primitive numeric types `Int`, `Long`, `Float`, and `Double`. Scala 3 allows to write numeric literals also for user-defined types. Example:
13+
1214
```scala
1315
val x: Long = -10_000_000_000
1416
val y: BigInt = 0x123_abc_789_def_345_678_901
@@ -17,6 +19,7 @@ val z: BigDecimal = 110_222_799_799.99
1719
(y: BigInt) match
1820
case 123_456_789_012_345_678_901 =>
1921
```
22+
2023
The syntax of numeric literals is the same as before, except there are no pre-set limits
2124
how large they can be.
2225

docs/docs/reference/changed-features/operators.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ a syntax change allows infix operators to be written on the left in a multi-line
1212
## The `infix` Modifier
1313

1414
An `infix` modifier on a method definition allows using the method as an infix operation. Example:
15+
1516
```scala
1617
import scala.annotation.targetName
1718

@@ -40,6 +41,7 @@ s1 * s2 // OK
4041
s1 `*` s2 // also OK, but unusual
4142
s1.*(s2) // also OK, but unusual
4243
```
44+
4345
Infix operations involving alphanumeric operators are deprecated, unless
4446
one of the following conditions holds:
4547

@@ -53,7 +55,8 @@ any Unicode character `c` for which `java.lang.Character.isIdentifierPart(c)` re
5355
Infix operations involving symbolic operators are always allowed, so `infix` is redundant for methods with symbolic names.
5456

5557
The `infix` modifier can also be given to a type:
56-
```
58+
59+
```scala
5760
infix type or[X, Y]
5861
val x: String or Int = ...
5962
```
@@ -107,6 +110,7 @@ It is recommended that definitions of symbolic operators carry a [`@targetName`
107110
## Syntax Change
108111

109112
Infix operators can now appear at the start of lines in a multi-line expression. Examples:
113+
110114
```scala
111115
val str = "hello"
112116
++ " world"
@@ -117,6 +121,7 @@ def condition =
117121
|| xs.exists(_ > 0)
118122
|| xs.isEmpty
119123
```
124+
120125
Previously, those expressions would have been rejected, since the compiler's semicolon inference
121126
would have treated the continuations `++ " world"` or `|| xs.isEmpty` as separate statements.
122127

@@ -133,19 +138,24 @@ Example:
133138
freezing
134139
| boiling
135140
```
141+
136142
This is recognized as a single infix operation. Compare with:
143+
137144
```scala
138145
freezing
139146
!boiling
140147
```
148+
141149
This is seen as two statements, `freezing` and `!boiling`. The difference is that only the operator in the first example
142150
is followed by a space.
143151

144152
Another example:
153+
145154
```scala
146155
println("hello")
147156
???
148157
??? match { case 0 => 1 }
149158
```
159+
150160
This code is recognized as three different statements. `???` is syntactically a symbolic identifier, but
151161
neither of its occurrences is followed by a space and a token that can start an expression.

docs/docs/reference/changed-features/structural-types-spec.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ the methods `selectDynamic` and `applyDynamic`. The methods could be members of
3030

3131
The `selectDynamic` method takes a field name and returns the value associated with that name in the `Selectable`.
3232
It should have a signature of the form:
33+
3334
```scala
3435
def selectDynamic(name: String): T
3536
```
37+
3638
Often, the return type `T` is `Any`.
3739

3840
Unlike `scala.Dynamic`, there is no special meaning for an `updateDynamic` method.
@@ -41,10 +43,12 @@ Consequently, it is recommended not to define any member called `updateDynamic`
4143

4244
The `applyDynamic` method is used for selections that are applied to arguments. It takes a method name and possibly `Class`es representing its parameters types as well as the arguments to pass to the function.
4345
Its signature should be of one of the two following forms:
46+
4447
```scala
4548
def applyDynamic(name: String)(args: Any*): T
4649
def applyDynamic(name: String, ctags: Class[?]*)(args: Any*): T
4750
```
51+
4852
Both versions are passed the actual arguments in the `args` parameter. The second version takes in addition a vararg argument of `java.lang.Class`es that identify the method's parameter classes. Such an argument is needed
4953
if `applyDynamic` is implemented using Java reflection, but it could be
5054
useful in other cases as well. `selectDynamic` and `applyDynamic` can also take additional context parameters in using clauses. These are resolved in the normal way at the callsite.
@@ -94,5 +98,4 @@ conversion that can turn `v` into a `Selectable`, and the selection methods coul
9498

9599
## Context
96100

97-
For more info, see [Rethink Structural
98-
Types](https://github.com/lampepfl/dotty/issues/1886).
101+
For more information, see [Rethink Structural Types](https://github.com/lampepfl/dotty/issues/1886).

docs/docs/reference/changed-features/structural-types.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,23 @@ configure how fields and methods should be resolved.
3232
## Example
3333

3434
Here's an example of a structural type `Person`:
35+
3536
```scala
3637
class Record(elems: (String, Any)*) extends Selectable:
3738
private val fields = elems.toMap
3839
def selectDynamic(name: String): Any = fields(name)
3940

4041
type Person = Record { val name: String; val age: Int }
4142
```
43+
4244
The type `Person` adds a _refinement_ to its parent type `Record` that defines the two fields `name` and `age`. We say the refinement is _structural_ since `name` and `age` are not defined in the parent type. But they exist nevertheless as members of class `Person`. For instance, the following
4345
program would print "Emma is 42 years old.":
46+
4447
```scala
4548
val person = Record("name" -> "Emma", "age" -> 42).asInstanceOf[Person]
4649
println(s"${person.name} is ${person.age} years old.")
4750
```
51+
4852
The parent type `Record` in this example is a generic class that can represent arbitrary records in its `elems` argument. This argument is a
4953
sequence of pairs of labels of type `String` and values of type `Any`.
5054
When we create a `Person` as a `Record` we have to assert with a typecast
@@ -59,19 +63,22 @@ a method `selectDynamic`, which maps a field name to its value.
5963
Selecting a structural type member is done by calling this method.
6064
The `person.name` and `person.age` selections are translated by
6165
the Scala compiler to:
66+
6267
```scala
6368
person.selectDynamic("name").asInstanceOf[String]
6469
person.selectDynamic("age").asInstanceOf[Int]
6570
```
6671

6772
Besides `selectDynamic`, a `Selectable` class sometimes also defines a method `applyDynamic`. This can then be used to translate function calls of structural members. So, if `a` is an instance of `Selectable`, a structural call like `a.f(b, c)` would translate to
73+
6874
```scala
6975
a.applyDynamic("f")(b, c)
7076
```
7177

7278
## Using Java Reflection
7379

7480
Structural types can also be accessed using [Java reflection](https://www.oracle.com/technical-resources/articles/java/javareflection.html). Example:
81+
7582
```scala
7683
type Closeable = { def close(): Unit }
7784

@@ -81,14 +88,17 @@ Structural types can also be accessed using [Java reflection](https://www.oracle
8188
class Channel:
8289
def close(): Unit
8390
```
91+
8492
Here, we define a structural type `Closeable` that defines a `close` method. There are various classes that have `close` methods, we just list `FileInputStream` and `Channel` as two examples. It would be easiest if the two classes shared a common interface that factors out the `close` method. But such factorings are often not possible if different libraries are combined in one application. Yet, we can still have methods that work on
8593
all classes with a `close` method by using the `Closeable` type. For instance,
94+
8695
```scala
8796
import scala.reflect.Selectable.reflectiveSelectable
8897

8998
def autoClose(f: Closeable)(op: Closeable => Unit): Unit =
9099
try op(f) finally f.close()
91100
```
101+
92102
The call `f.close()` has to use Java reflection to identify and call the `close` method in the receiver `f`. This needs to be enabled by an import
93103
of `reflectiveSelectable` shown above. What happens "under the hood" is then the following:
94104

@@ -122,6 +132,7 @@ the database access example given at the beginning of this document.
122132

123133
Local and anonymous classes that extend `Selectable` get more refined types
124134
than other classes. Here is an example:
135+
125136
```scala
126137
trait Vehicle extends reflect.Selectable:
127138
val wheels: Int
@@ -132,12 +143,14 @@ val i3 = new Vehicle: // i3: Vehicle { val range: Int }
132143

133144
i3.range
134145
```
146+
135147
The type of `i3` in this example is `Vehicle { val range: Int }`. Hence,
136148
`i3.range` is well-formed. Since the base class `Vehicle` does not define a `range` field or method, we need structural dispatch to access the `range` field of the anonymous class that initializes `id3`. Structural dispatch
137149
is implemented by the base trait `reflect.Selectable` of `Vehicle`, which
138150
defines the necessary `selectDynamic` member.
139151

140152
`Vehicle` could also extend some other subclass of `scala.Selectable` that implements `selectDynamic` and `applyDynamic` differently. But if it does not extend a `Selectable` at all, the code would no longer typecheck:
153+
141154
```scala
142155
trait Vehicle:
143156
val wheels: Int
@@ -148,6 +161,7 @@ val i3 = new Vehicle: // i3: Vehicle
148161

149162
i3.range // error: range is not a member of `Vehicle`
150163
```
164+
151165
The difference is that the type of an anonymous class that does not extend `Selectable` is just formed from the parent type(s) of the class, without
152166
adding any refinements. Hence, `i3` now has just type `Vehicle` and the selection `i3.range` gives a "member not found" error.
153167

docs/docs/reference/changed-features/type-checking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ layout: doc-page
33
title: "Changes in Type Checking"
44
---
55

6-
[//]: # todo: fill in
6+
*** **TO BE FILLED IN** ***

docs/docs/reference/changed-features/type-inference.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ layout: doc-page
33
title: "Changes in Type Inference"
44
---
55

6-
See https://www.youtube.com/watch?v=lMvOykNQ4zs, https://www.youtube.com/watch?v=VV9lPg3fNl8.
6+
For more information, see the two presentations
7+
8+
* [Scala 3, Type inference and You!](https://www.youtube.com/watch?v=lMvOykNQ4zs) by Guillaume Martres (September 2019)
9+
* [GADTs in Dotty](https://www.youtube.com/watch?v=VV9lPg3fNl8) by Aleksander Boruch-Gruszecki (July 2019).

docs/docs/reference/changed-features/wildcards.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ for the type lambda `[X] =>> C[X]`. This makes higher-kinded types easier to use
1717
parameter, `F[_]` means `F` is a type constructor whereas used as a type, `F[_]` means it is a wildcard (i.e. existential) type.
1818
In the future, `F[_]` will mean the same thing, no matter where it is used.
1919

20-
We pick `?` as a replacement syntax for wildcard types, since it aligns with Java's syntax.
20+
We pick `?` as a replacement syntax for wildcard types, since it aligns with
21+
[Java's syntax](https://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html).
2122

2223
### Migration Strategy
2324

@@ -40,4 +41,4 @@ option `-Ykind-projector`:
4041
available to rewrite one to the other.
4142
3. In Scala 3.3, `*` is removed again, and all type parameter placeholders will be expressed with `_`.
4243

43-
These rules make it possible to cross build between Scala 2 using the kind projector plugin and Scala 3.0 - 3.2 using option `-Ykind-projector`.
44+
These rules make it possible to cross build between Scala 2 using the kind projector plugin and Scala 3.0 - 3.2 using the compiler option `-Ykind-projector`.

docs/docs/reference/contextual/by-name-context-parameters.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The precise steps for synthesizing an argument for a by-name context parameter o
3535
```scala
3636
given lv: T = ???
3737
```
38+
3839
where `lv` is an arbitrary fresh name.
3940

4041
1. This given is not immediately available as candidate for argument inference (making it immediately available could result in a loop in the synthesized computation). But it becomes available in all nested contexts that look again for an argument to a by-name context parameter.
@@ -51,13 +52,13 @@ In the example above, the definition of `s` would be expanded as follows.
5152

5253
```scala
5354
val s = summon[Test.Codec[Option[Int]]](
54-
optionCodec[Int](using intCodec)
55+
optionCodec[Int](using intCodec)
5556
)
5657
```
5758

5859
No local given instance was generated because the synthesized argument is not recursive.
5960

6061
### Reference
6162

62-
For more info, see [Issue #1998](https://github.com/lampepfl/dotty/issues/1998)
63+
For more information, see [Issue #1998](https://github.com/lampepfl/dotty/issues/1998)
6364
and the associated [Scala SIP](https://docs.scala-lang.org/sips/byname-implicits.html).

0 commit comments

Comments
 (0)