You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/metaprogramming/inline.md
+18-16Lines changed: 18 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -170,17 +170,17 @@ Inline methods can override other non-inline methods. The rules are as follows:
170
170
2. Inline methods are effectively final.
171
171
172
172
3. Inline methods can also be abstract. Anabstract inline method can be implemented only by other inline methods. It cannot be invoked directly:
173
-
```scala
174
-
abstractclassA {
175
-
inlinedeff():Int
176
-
}
177
-
objectBextendsA {
178
-
inlinedeff():Int=22
179
-
}
180
-
B.f() // OK
181
-
vala:A=B
182
-
a.f() // error: cannot inline f() in A.
183
-
```
173
+
```scala
174
+
abstractclassA {
175
+
inlinedeff():Int
176
+
}
177
+
objectBextendsA {
178
+
inlinedeff():Int=22
179
+
}
180
+
B.f() // OK
181
+
vala:A=B
182
+
a.f() // error: cannot inline f() in A.
183
+
```
184
184
185
185
###Relationship to @inline
186
186
@@ -241,18 +241,20 @@ class B extends A {
241
241
}
242
242
243
243
transparent inline def choose(b: Boolean): A =
244
-
if b thenA()elseB
244
+
if b then A else B
245
245
246
246
val obj1 = choose(true) // static type is A
247
247
val obj2 = choose(false) // static type is B
248
248
249
249
// obj1.m() // compile-time error: `m` is not defined on `A`
250
250
obj2.m() // OK
251
251
```
252
-
Here, the inline method `choose` returns an object of either of the two types `A` and `B`. If `choose` had been declared with a normal return type `: A`, the result
253
-
of its expansion would always be of type `A`, even though the computed value might be of the subtype `B`. The inline method is a "blackbox" in the sense that details of its implementation do not leak out. But if a `transparent` modifier is given,
254
-
the expansion is the type of the expanded body. If the argument `b`
255
-
is `true`, that type is `A`, otherwise it is `B`. Consequently, calling `meth` on `obj2`
252
+
Here, the inline method `choose` returns an instance of either of the two types `A` or `B`.
253
+
If `choose` had not been declared to be `transparent`, the result
254
+
of its expansion would always be of type `A`, even though the computed value might be of the subtype `B`.
255
+
The inline method is a "blackbox" in the sense that details of its implementation do not leak out.
256
+
But if a `transparent` modifier is given, the expansion is the type of the expanded body. If the argument `b`
257
+
is `true`, that type is `A`, otherwise it is `B`. Consequently, calling `m` on `obj2`
256
258
type-checks since `obj2` has the same type as the expansion of `choose(false)`, which is `B`.
257
259
Transparent inline methods are "whitebox" in the sense that the type
258
260
of an application of such a method can be more specialized than its declared
0 commit comments