Skip to content

Commit 7436304

Browse files
committed
Change snippet appearance. Add buttons and labels. UX improvements.
1 parent bf7cf83 commit 7436304

File tree

18 files changed

+399
-126
lines changed

18 files changed

+399
-126
lines changed

docs/docs/reference/other-new-features/creator-applications.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ function application, without needing to write `new`.
99

1010
Scala 3 generalizes this scheme to all concrete classes. Example:
1111

12-
```scala sc-id:1
12+
```scala sc-name:Base.scala
1313
class MyStringBuilder(s: String):
1414
def this() = this("")
1515
```
1616

17-
```scala sc-compile-with:1
17+
```scala sc-compile-with:Base.scala
1818
MyStringBuilder("abc") // old: new MyStringBuilder("abc")
1919
MyStringBuilder() // old: new MyStringBuilder()
2020
```
2121

2222
This works since a companion object with two `apply` methods
2323
is generated together with the class. The object looks like this:
2424

25-
```scala sc-compile-with:1
25+
```scala sc-compile-with:Base.scala
2626
object MyStringBuilder:
2727
inline def apply(s: String): MyStringBuilder = new MyStringBuilder(s)
2828
inline def apply(): MyStringBuilder = new MyStringBuilder()

docs/docs/reference/other-new-features/explicit-nulls.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ We illustrate the rules with following examples:
148148

149149
==>
150150

151-
```scala sc-id:1
151+
```scala sc-name:C.scala
152152
class C[T] { def foo(): T | Null = null }
153153
```
154154

155155
Notice this is rule is sometimes too conservative, as witnessed by
156156

157-
```scala sc:fail sc-compile-with:1
157+
```scala sc:fail sc-compile-with:C.scala
158158
class InScala:
159159
val c: C[Boolean] = ??? // C as above
160160
val b: Boolean = c.foo() // no longer typechecks, since foo now returns Bool | Null
@@ -169,11 +169,11 @@ We illustrate the rules with following examples:
169169

170170
==>
171171

172-
```scala sc-id:2
172+
```scala sc-name:Box.scala
173173
abstract class Box[T] { def get(): T | Null }
174174
```
175175

176-
```scala sc-compile-with:2
176+
```scala sc-compile-with:Box.scala
177177
abstract class BoxFactory[T] { def makeBox(): Box[T] | Null }
178178
```
179179

@@ -198,7 +198,7 @@ We illustrate the rules with following examples:
198198

199199
==>
200200

201-
```scala sc-compile-with:2
201+
```scala sc-compile-with:Box.scala
202202
abstract class BoxFactory[T]:
203203
def makeBox(): Box[T | Null] | Null
204204
def makeCrazyBoxes(): java.util.List[Box[java.util.List[T] | Null]] | Null
@@ -251,7 +251,7 @@ We illustrate the rules with following examples:
251251

252252
==>
253253

254-
```scala sc-compile-with:2
254+
```scala sc-compile-with:Box.scala
255255
abstract class C:
256256
val name: String
257257
def getNames(prefix: String | Null): java.util.List[String] // we still need to nullify the paramter types

docs/docs/reference/other-new-features/export.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,19 @@ ImportSelectors ::= NamedSelector [‘,’ ImportSelectors]
142142
Export clauses raise questions about the order of elaboration during type checking.
143143
Consider the following example:
144144
145-
```scala sc-id:1
145+
```scala sc-name:Base.scala
146146
class B { val c: Int = ??? }
147147
object a { val b = new B }
148148
```
149149

150-
```scala sc-compile-with:1
150+
```scala sc-compile-with:Base.scala
151151
export a.*
152152
export b.*
153153
```
154154

155155
Is the `export b.*` clause legal? If yes, what does it export? Is it equivalent to `export a.b.*`? What about if we swap the last two clauses?
156156

157-
```scala sc:fail sc-compile-with:1
157+
```scala sc:fail sc-compile-with:Base.scala
158158
export b.*
159159
export a.*
160160
```

docs/docs/reference/other-new-features/indentation-experimental.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ IndentedArgument ::= indent (CaseClauses | Block) outdent
9292
Note that a lambda argument must have the `=>` at the end of a line for braces
9393
to be optional. For instance, the following would also be incorrect:
9494

95-
```scala sc-id:1
95+
```scala sc-name:Base.scala
9696
val xs: Seq[Int]
9797
```
9898

99-
```scala sc:fail sc-compile-with:1
99+
```scala sc:fail sc-compile-with:Base.scala
100100
xs.map x => x + 1 // error: braces or parentheses are required
101101
```
102102
The lambda has to be enclosed in braces or parentheses:
103-
```scala sc-compile-with:1
103+
```scala sc-compile-with:Base.scala
104104
xs.map(x => x + 1) // ok
105105
```

docs/docs/reference/other-new-features/matchable.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ is guaranteed to succeed at run-time since `Any` and `Matchable` both erase to
120120

121121
For instance, consider the definitions
122122

123-
```scala sc-id:1
123+
```scala sc-name:Meter.scala
124124
opaque type Meter = Double
125125
def Meter(x: Double) = x
126126

@@ -130,13 +130,13 @@ def Second(x: Double) = x
130130

131131
Here, universal `equals` will return true for
132132

133-
```scala sc-compile-with:1
133+
```scala sc-compile-with:Meter.scala
134134
Meter(10).equals(Second(10))
135135
```
136136

137137
even though this is clearly false mathematically. With [multiversal equality](../contextual/multiversal-equality.md) one can mitigate that problem somewhat by turning
138138

139-
```scala sc-compile-with:1
139+
```scala sc-compile-with:Meter.scala
140140
Meter(10) == Second(10)
141141
```
142142

docs/docs/reference/other-new-features/opaques.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ movedTo: https://docs.scala-lang.org/scala3/reference/other-new-features/opaques
66

77
Opaque types aliases provide type abstraction without any overhead. Example:
88

9-
```scala sc-id:1
9+
```scala sc-name:MyMath.scala
1010
object MyMath:
1111

1212
opaque type Logarithm = Double
@@ -41,7 +41,7 @@ The public API of `Logarithm` consists of the `apply` and `safe` methods defined
4141
They convert from `Double`s to `Logarithm` values. Moreover, an operation `toDouble` that converts the other way, and operations `+` and `*` are defined as extension methods on `Logarithm` values.
4242
The following operations would be valid because they use functionality implemented in the `MyMath` object.
4343

44-
```scala sc-compile-with:1
44+
```scala sc-compile-with:MyMath.scala
4545
import MyMath.Logarithm
4646

4747
val l = Logarithm(1.0)
@@ -52,7 +52,7 @@ val l4 = l + l2
5252

5353
But the following operations would lead to type errors:
5454

55-
```scala sc:fail sc-compile-with:1
55+
```scala sc:fail sc-compile-with:MyMath.scala
5656
import MyMath.Logarithm
5757

5858
val l = Logarithm(1.0)
@@ -66,7 +66,7 @@ l / l2 // error: `/` is not a member of Logarithm
6666

6767
Opaque type aliases can also come with bounds. Example:
6868

69-
```scala sc-id:2
69+
```scala sc-name:Access.scala
7070
object Access:
7171

7272
opaque type Permissions = Int
@@ -113,7 +113,7 @@ All three opaque type aliases have the same underlying representation type `Int`
113113
it known outside the `Access` object that `Permission` is a subtype of the other
114114
two types. Hence, the following usage scenario type-checks.
115115

116-
```scala sc-compile-with:2
116+
```scala sc-compile-with:Access.scala
117117
object User:
118118
import Access.*
119119

@@ -142,7 +142,7 @@ since `Permissions` and `PermissionChoice` are different, unrelated types outsid
142142
While typically, opaque types are used together with objects to hide implementation details of a module, they can also be used with classes.
143143

144144
For example, we can redefine the above example of Logarithms as a class.
145-
```scala sc-id:3
145+
```scala sc-name:Logarithms.scala
146146
class Logarithms:
147147

148148
opaque type Logarithm = Double
@@ -156,7 +156,7 @@ class Logarithms:
156156
```
157157

158158
Opaque type members of different instances are treated as different:
159-
```scala sc:fail sc-compile-with:3
159+
```scala sc:fail sc-compile-with:Logarithms.scala
160160
val l1 = new Logarithms
161161
val l2 = new Logarithms
162162
val x = l1(1.5)

docs/docs/reference/other-new-features/parameter-untupling-spec.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ movedTo: https://docs.scala-lang.org/scala3/reference/other-new-features/paramet
77
## Motivation
88
Say you have a list of pairs
99

10-
```scala sc-id:1
10+
```scala sc-name:Base.scala
1111
val xs: List[(Int, Int)]
1212
```
1313

1414
and you want to map `xs` to a list of `Int`s so that each pair of numbers is mapped to their sum.
1515
Previously, the best way to do this was with a pattern-matching decomposition:
1616

17-
```scala sc-compile-with:1
17+
```scala sc-compile-with:Base.scala
1818
xs.map {
1919
case (x, y) => x + y
2020
}
2121
```
2222
While correct, this is inconvenient. Instead, we propose to write it the following way:
2323

24-
```scala sc-compile-with:1
24+
```scala sc-compile-with:Base.scala
2525
xs.map {
2626
(x, y) => x + y
2727
}
2828
```
2929

3030
or, equivalently:
3131

32-
```scala sc-compile-with:1
32+
```scala sc-compile-with:Base.scala
3333
xs.map(_ + _)
3434
```
3535

docs/docs/reference/other-new-features/parameter-untupling.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ movedTo: https://docs.scala-lang.org/scala3/reference/other-new-features/paramet
66

77
Say you have a list of pairs
88

9-
```scala sc-id:1
9+
```scala sc-name:Base.scala
1010
val xs: List[(Int, Int)]
1111
```
1212

1313
and you want to map `xs` to a list of `Int`s so that each pair of numbers is mapped to
1414
their sum. Previously, the best way to do this was with a pattern-matching decomposition:
1515

16-
```scala sc-compile-with:1
16+
```scala sc-compile-with:Base.scala
1717
xs map {
1818
case (x, y) => x + y
1919
}
@@ -22,15 +22,15 @@ xs map {
2222
While correct, this is also inconvenient and confusing, since the `case`
2323
suggests that the pattern match could fail. As a shorter and clearer alternative Scala 3 now allows
2424

25-
```scala sc-compile-with:1
25+
```scala sc-compile-with:Base.scala
2626
xs.map {
2727
(x, y) => x + y
2828
}
2929
```
3030

3131
or, equivalently:
3232

33-
```scala sc-compile-with:1
33+
```scala sc-compile-with:Base.scala
3434
xs.map(_ + _)
3535
```
3636

docs/docs/reference/other-new-features/trait-parameters.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ movedTo: https://docs.scala-lang.org/scala3/reference/other-new-features/trait-p
66

77
Scala 3 allows traits to have parameters, just like classes have parameters.
88

9-
```scala sc-id:1
9+
```scala sc-name:Greeting.scala
1010
trait Greeting(val name: String):
1111
def msg = s"How are you, $name"
1212

@@ -20,7 +20,7 @@ One potential issue with trait parameters is how to prevent
2020
ambiguities. For instance, you might try to extend `Greeting` twice,
2121
with different parameters.
2222

23-
```scala sc:fail sc-compile-with:1
23+
```scala sc:fail sc-compile-with:Greeting.scala
2424
class D extends C, Greeting("Bill") // error: parameter passed twice
2525
```
2626

@@ -35,21 +35,21 @@ because it violates the second rule of the following for trait parameters:
3535

3636
Here's a trait extending the parameterized trait `Greeting`.
3737

38-
```scala sc-compile-with:1 sc-id:2
38+
```scala sc-compile-with:Greeting.scala sc-name:FormalGreeting.scala
3939
trait FormalGreeting extends Greeting:
4040
override def msg = s"How do you do, $name"
4141
```
4242
As is required, no arguments are passed to `Greeting`. However, this poses an issue
4343
when defining a class that extends `FormalGreeting`:
4444

45-
```scala sc:fail sc-compile-with:2
45+
```scala sc:fail sc-compile-with:FormalGreeting.scala
4646
class E extends FormalGreeting // error: missing arguments for `Greeting`.
4747
```
4848

4949
The correct way to write `E` is to extend both `Greeting` and
5050
`FormalGreeting` (in either order):
5151

52-
```scala sc-compile-with:2
52+
```scala sc-compile-with:FormalGreeting.scala
5353
class E extends Greeting("Bob"), FormalGreeting
5454
```
5555

0 commit comments

Comments
 (0)