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+
importjava.io.IOException
13+
typeT
14+
varx:Int
15+
varxs:List[Int]
16+
varys:List[Int]
17+
varbody:T
18+
varhandle:T
19+
deff(x: Int):Int=???
20+
//}
1121
if x <0then
1222
"negative"
1323
elseif x ==0then

‎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-
classStringBuilder(s: String):
12+
```scala sc-name:Base.scala
13+
classMyStringBuilder(s: String):
1414
defthis() =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-
objectStringBuilder:
25-
inlinedefapply(s: String):StringBuilder=newStringBuilder(s)
26-
inlinedefapply():StringBuilder=newStringBuilder()
25+
```scala sc-compile-with:Base.scala
26+
objectMyStringBuilder:
27+
inlinedefapply(s: String):MyStringBuilder=newMyStringBuilder(s)
28+
inlinedefapply():MyStringBuilder=newMyStringBuilder()
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+
valx: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-
classC{
129+
abstractclassC{
127130
String s;
128131
int x;
129132
}
@@ -132,7 +135,7 @@ We illustrate the rules with following examples:
132135
==>
133136

134137
```scala
135-
classC:
138+
abstractclassC:
136139
vals:String|Null
137140
valx:Int
138141
```
@@ -145,30 +148,33 @@ We illustrate the rules with following examples:
145148

146149
==>
147150

148-
```scala
149-
classC[T]{deffoo():T|Null }
151+
```scala sc-name:C.scala
152+
classC[T]{deffoo():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
classInScala:
156-
valc:C[Bool] =???// C as above
157-
valb:Bool= c.foo() // no longer typechecks, since foo now returns Bool | Null
159+
valc:C[Boolean] =???// C as above
160+
valb: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-
classBox<T>{Tget()}
164-
classBoxFactory<T>{Box<T>makeBox()}
166+
abstractclassBox<T>{Tget()}
167+
abstractclassBoxFactory<T>{Box<T>makeBox()}
165168
```
166169

167170
==>
168171

169-
```scala
170-
classBox[T]{defget():T|Null }
171-
classBoxFactory[T]{defmakeBox():Box[T] |Null }
172+
```scala sc-name:Box.scala
173+
abstractclassBox[T]{defget():T|Null }
174+
```
175+
176+
```scala sc-compile-with:Box.scala
177+
abstractclassBoxFactory[T]{defmakeBox():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-
classBoxFactory<T>{
193+
abstractclassBoxFactory<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-
classBoxFactory[T]:
201+
```scala sc-compile-with:Box.scala
202+
abstractclassBoxFactory[T]:
197203
defmakeBox():Box[T|Null] |Null
198204
defmakeCrazyBoxes(): 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+
defgetNewName():String=???
232+
//}
224233
classConstants:
225-
valNAME:String("name")="name"
226-
valAGE:Int(0)=0
227-
valCHAR:Char('a')='a'
234+
valNAME:String="name"
235+
valAGE:Int=0
236+
valCHAR:Char='a'
228237

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

243252
==>
244253

245-
```scala
246-
classC:
254+
```scala sc-compile-with:Box.scala
255+
abstractclassC:
247256
valname:String
248257
defgetNames(prefix: String|Null): java.util.List[String] // we still need to nullify the paramter types
249258
defgetBoxedName():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
deff(x: String|Null):String|Null
295+
```
286296

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

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

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

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

305320
```scala
306321
vals:String|Null=???
307-
if s !=nullthen
322+
if s !=nullthen???
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+
vals:String|Null=???
319335
if s ==nullthen
336+
???
320337
// s: String | Null
321338
else
339+
???
322340
// s: String
323341
```
324342

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

338357
if s ==null|| s2 ==nullthen
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
vals:String|Null=???
352373

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

356378
if s ==null|| s.length >0then// 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
vals:String|Null=???
443467
vals2: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
classBitMap
1111
classInkJet
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
finaldefscan():BitMap= scanUnit.scan()
3636
finaldefprint(bits: BitMap):Unit= printUnit.print(bits)
3737
finaltypePrinterType= 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
valcopier=newCopier
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
exportpath .{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
classC{typeT }
89-
objectO{valc:C=... }
89+
objectO{valc:C=??? }
9090
exportO.c
91-
deff: c.T=...
91+
deff: 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
exportstatusasstat
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
exporta.*
149152
exportb.*
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
exportb.*
156159
exporta.*
157160
```

0 commit comments

Comments
(0)