Skip to content

Commit 05fd457

Browse files
committed
Fix snippets in Other new features and Usage section
1 parent e59c5ea commit 05fd457

25 files changed

+378
-216
lines changed

docs/docs/reference/other-new-features/control-syntax.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ Scala 3 has a new "quiet" syntax for control expressions that does not rely on
88
enclosing the condition in parentheses, and also allows to drop parentheses or braces
99
around the generators of a `for`-expression. Examples:
1010
```scala
11+
//{
12+
import java.io.IOException
13+
type T
14+
var x: Int
15+
var xs: List[Int]
16+
var ys: List[Int]
17+
var body: T
18+
var handle: T
19+
def f(x: Int): Int = ???
20+
//}
1121
if x < 0 then
1222
"negative"
1323
else if x == 0 then

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

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

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

12-
```scala
13-
class StringBuilder(s: String):
12+
```scala sc-name:Base.scala
13+
class MyStringBuilder(s: String):
1414
def this() = this("")
15+
```
1516

16-
StringBuilder("abc") // old: new StringBuilder("abc")
17-
StringBuilder() // old: new StringBuilder()
17+
```scala sc-compile-with:Base.scala
18+
MyStringBuilder("abc") // old: new MyStringBuilder("abc")
19+
MyStringBuilder() // old: new MyStringBuilder()
1820
```
1921

2022
This works since a companion object with two `apply` methods
2123
is generated together with the class. The object looks like this:
2224

23-
```scala
24-
object StringBuilder:
25-
inline def apply(s: String): StringBuilder = new StringBuilder(s)
26-
inline def apply(): StringBuilder = new StringBuilder()
25+
```scala sc-compile-with:Base.scala
26+
object MyStringBuilder:
27+
inline def apply(s: String): MyStringBuilder = new MyStringBuilder(s)
28+
inline def apply(): MyStringBuilder = new MyStringBuilder()
2729
```
2830

29-
The synthetic object `StringBuilder` and its `apply` methods are called _constructor proxies_.
31+
The synthetic object `MyStringBuilder` and its `apply` methods are called _constructor proxies_.
3032
Constructor proxies are generated even for Java classes and classes coming from Scala 2.
3133
The precise rules are as follows:
3234

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

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ val x: String | Null = null // ok
2222
A nullable type could have null value during runtime; hence, it is not safe to select a member without checking its nullity.
2323

2424
```scala
25+
//{
26+
val x: String | Null
27+
//}
2528
x.trim // error: trim is not member of String | Null
2629
```
2730

@@ -123,7 +126,7 @@ We illustrate the rules with following examples:
123126
- The first two rules are easy: we nullify reference types but not value types.
124127

125128
```java
126-
class C {
129+
abstract class C {
127130
String s;
128131
int x;
129132
}
@@ -132,7 +135,7 @@ We illustrate the rules with following examples:
132135
==>
133136

134137
```scala
135-
class C:
138+
abstract class C:
136139
val s: String | Null
137140
val x: Int
138141
```
@@ -145,30 +148,33 @@ We illustrate the rules with following examples:
145148

146149
==>
147150

148-
```scala
149-
class C[T] { def foo(): T | Null }
151+
```scala sc-name:C.scala
152+
class C[T] { def foo(): T | Null = null }
150153
```
151154

152155
Notice this is rule is sometimes too conservative, as witnessed by
153156

154-
```scala
157+
```scala sc:fail sc-compile-with:C.scala
155158
class InScala:
156-
val c: C[Bool] = ??? // C as above
157-
val b: Bool = c.foo() // no longer typechecks, since foo now returns Bool | Null
159+
val c: C[Boolean] = ??? // C as above
160+
val b: Boolean = c.foo() // no longer typechecks, since foo now returns Bool | Null
158161
```
159162

160163
- We can reduce the number of redundant nullable types we need to add. Consider
161164

162165
```java
163-
class Box<T> { T get(); }
164-
class BoxFactory<T> { Box<T> makeBox(); }
166+
abstract class Box<T> { T get(); }
167+
abstract class BoxFactory<T> { Box<T> makeBox(); }
165168
```
166169

167170
==>
168171

169-
```scala
170-
class Box[T] { def get(): T | Null }
171-
class BoxFactory[T] { def makeBox(): Box[T] | Null }
172+
```scala sc-name:Box.scala
173+
abstract class Box[T] { def get(): T | Null }
174+
```
175+
176+
```scala sc-compile-with:Box.scala
177+
abstract class BoxFactory[T] { def makeBox(): Box[T] | Null }
172178
```
173179

174180
Suppose we have a `BoxFactory[String]`. Notice that calling `makeBox()` on it returns a
@@ -184,16 +190,16 @@ We illustrate the rules with following examples:
184190
- We will append `Null` to the type arguments if the generic class is defined in Scala.
185191

186192
```java
187-
class BoxFactory<T> {
193+
abstract class BoxFactory<T> {
188194
Box<T> makeBox(); // Box is Scala-defined
189195
List<Box<List<T>>> makeCrazyBoxes(); // List is Java-defined
190196
}
191197
```
192198

193199
==>
194200

195-
```scala
196-
class BoxFactory[T]:
201+
```scala sc-compile-with:Box.scala
202+
abstract class BoxFactory[T]:
197203
def makeBox(): Box[T | Null] | Null
198204
def makeCrazyBoxes(): java.util.List[Box[java.util.List[T] | Null]] | Null
199205
```
@@ -221,10 +227,13 @@ We illustrate the rules with following examples:
221227
==>
222228

223229
```scala
230+
//{
231+
def getNewName(): String = ???
232+
//}
224233
class Constants:
225-
val NAME: String("name") = "name"
226-
val AGE: Int(0) = 0
227-
val CHAR: Char('a') = 'a'
234+
val NAME: String = "name"
235+
val AGE: Int = 0
236+
val CHAR: Char = 'a'
228237

229238
val NAME_GENERATED: String | Null = getNewName()
230239
```
@@ -242,8 +251,8 @@ We illustrate the rules with following examples:
242251

243252
==>
244253

245-
```scala
246-
class C:
254+
```scala sc-compile-with:Box.scala
255+
abstract class C:
247256
val name: String
248257
def getNames(prefix: String | Null): java.util.List[String] // we still need to nullify the paramter types
249258
def getBoxedName(): Box[String | Null] // we don't append `Null` to the outmost level, but we still need to nullify inside
@@ -252,7 +261,7 @@ We illustrate the rules with following examples:
252261
The annotation must be from the list below to be recognized as `NotNull` by the compiler.
253262
Check `Definitions.scala` for an updated list.
254263

255-
```scala
264+
```scala sc:nocompile
256265
// A list of annotations that are commonly used to indicate
257266
// that a field/method argument or return type is not null.
258267
// These annotations are used by the nullification logic in
@@ -283,11 +292,17 @@ Suppose we have Java method `String f(String x)`, we can override this method in
283292

284293
```scala
285294
def f(x: String | Null): String | Null
295+
```
286296

297+
```scala
287298
def f(x: String): String | Null
299+
```
288300

301+
```scala
289302
def f(x: String | Null): String
303+
```
290304

305+
```scala
291306
def f(x: String): String
292307
```
293308

@@ -304,7 +319,7 @@ Example:
304319

305320
```scala
306321
val s: String | Null = ???
307-
if s != null then
322+
if s != null then ???
308323
// s: String
309324

310325
// s: String | Null
@@ -316,9 +331,12 @@ assert(s != null)
316331
A similar inference can be made for the `else` case if the test is `p == null`
317332

318333
```scala
334+
val s: String | Null = ???
319335
if s == null then
336+
???
320337
// s: String | Null
321338
else
339+
???
322340
// s: String
323341
```
324342

@@ -332,13 +350,16 @@ We also support logical operators (`&&`, `||`, and `!`):
332350
val s: String | Null = ???
333351
val s2: String | Null = ???
334352
if s != null && s2 != null then
353+
???
335354
// s: String
336355
// s2: String
337356

338357
if s == null || s2 == null then
358+
???
339359
// s: String | Null
340360
// s2: String | Null
341361
else
362+
???
342363
// s: String
343364
// s2: String
344365
```
@@ -351,11 +372,14 @@ We also support type specialization _within_ the condition, taking into account
351372
val s: String | Null = ???
352373

353374
if s != null && s.length > 0 then // s: String in `s.length > 0`
375+
???
354376
// s: String
355377

356378
if s == null || s.length > 0 then // s: String in `s.length > 0`
379+
???
357380
// s: String | Null
358381
else
382+
???
359383
// s: String
360384
```
361385

@@ -442,6 +466,7 @@ We don't support:
442466
val s: String | Null = ???
443467
val s2: String | Null = ???
444468
if s != null && s == s2 then
469+
???
445470
// s: String inferred
446471
// s2: String not inferred
447472
```

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

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

77
An export clause defines aliases for selected members of an object. Example:
88

9-
```scala
9+
```scala sc-name:Model.scala
1010
class BitMap
1111
class InkJet
1212

@@ -31,22 +31,22 @@ class Copier:
3131

3232
The two `export` clauses define the following _export aliases_ in class `Copier`:
3333

34-
```scala
34+
```scala sc:nocompile
3535
final def scan(): BitMap = scanUnit.scan()
3636
final def print(bits: BitMap): Unit = printUnit.print(bits)
3737
final type PrinterType = printUnit.PrinterType
3838
```
3939

4040
They can be accessed inside `Copier` as well as from outside:
4141

42-
```scala
42+
```scala sc-compile-with:Model.scala
4343
val copier = new Copier
4444
copier.print(copier.scan())
4545
```
4646

4747
An export clause has the same format as an import clause. Its general form is:
4848

49-
```scala
49+
```scala sc:nocompile
5050
export path . { sel_1, ..., sel_n }
5151
```
5252

@@ -86,9 +86,9 @@ referring to private values in the qualifier path
8686
are marked by the compiler as "stable" and their result types are the singleton types of the aliased definitions. This means that they can be used as parts of stable identifier paths, even though they are technically methods. For instance, the following is OK:
8787
```scala
8888
class C { type T }
89-
object O { val c: C = ... }
89+
object O { val c: C = ??? }
9090
export O.c
91-
def f: c.T = ...
91+
def f: c.T = ???
9292
```
9393

9494

@@ -98,7 +98,7 @@ def f: c.T = ...
9898
1. If an export clause contains a wildcard or given selector, it is forbidden for its qualifier path to refer to a package. This is because it is not yet known how to safely track wildcard dependencies to a package for the purposes of incremental compilation.
9999

100100
1. Simple renaming exports like
101-
```scala
101+
```scala sc:nocompile
102102
export status as stat
103103
```
104104
are not supported yet. They would run afoul of the restriction that the
@@ -142,16 +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
146-
class B { val c: Int }
145+
```scala sc-name:Base.scala
146+
class B { val c: Int = ??? }
147147
object a { val b = new B }
148+
```
149+
150+
```scala sc-compile-with:Base.scala
148151
export a.*
149152
export b.*
150153
```
151154

152155
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?
153156

154-
```
157+
```scala sc:fail sc-compile-with:Base.scala
155158
export b.*
156159
export a.*
157160
```

0 commit comments

Comments
 (0)