Skip to content

Commit ec8d439

Browse files
authored
Merge pull request #8848 from tegonal/doc-inline
doc(inline): fix listing in override and fixes in transparent section
2 parents d8ac48c + 7b644ef commit ec8d439

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,40 +147,40 @@ funkyAssertEquals(computeActual(), computeExpected(), computeDelta())
147147
Inline methods can override other non-inline methods. The rules are as follows:
148148

149149
1. If an inline method `f` implements or overrides another, non-inline method, the inline method can also be invoked at runtime. For instance, consider the scenario:
150-
```scala
151-
abstract class A {
152-
def f(): Int
153-
def g(): Int = f()
154-
}
155-
class B extends A {
156-
inline def f() = 22
157-
override inline def g() = f() + 11
158-
}
159-
val b = B()
160-
val a: A = b
161-
// inlined invocatons
162-
assert(b.f() == 22)
163-
assert(b.g() == 33)
164-
// dynamic invocations
165-
assert(a.f() == 22)
166-
assert(a.g() == 33)
167-
```
168-
The inlined invocations and the dynamically dispatched invocations give the same results.
150+
```scala
151+
abstract class A {
152+
def f(): Int
153+
def g(): Int = f()
154+
}
155+
class B extends A {
156+
inline def f() = 22
157+
override inline def g() = f() + 11
158+
}
159+
val b = B()
160+
val a: A = b
161+
// inlined invocatons
162+
assert(b.f() == 22)
163+
assert(b.g() == 33)
164+
// dynamic invocations
165+
assert(a.f() == 22)
166+
assert(a.g() == 33)
167+
```
168+
The inlined invocations and the dynamically dispatched invocations give the same results.
169169

170170
2. Inline methods are effectively final.
171171

172172
3. Inline methods can also be abstract. An abstract inline method can be implemented only by other inline methods. It cannot be invoked directly:
173-
```scala
174-
abstract class A {
175-
inline def f(): Int
176-
}
177-
object B extends A {
178-
inline def f(): Int = 22
179-
}
180-
B.f() // OK
181-
val a: A = B
182-
a.f() // error: cannot inline f() in A.
183-
```
173+
```scala
174+
abstract class A {
175+
inline def f(): Int
176+
}
177+
object B extends A {
178+
inline def f(): Int = 22
179+
}
180+
B.f() // OK
181+
val a: A = B
182+
a.f() // error: cannot inline f() in A.
183+
```
184184

185185
### Relationship to @inline
186186

@@ -260,18 +260,20 @@ class B extends A {
260260
}
261261

262262
transparent inline def choose(b: Boolean): A =
263-
if b then A() else B
263+
if b then new A() else new B()
264264

265265
val obj1 = choose(true) // static type is A
266266
val obj2 = choose(false) // static type is B
267267

268268
// obj1.m() // compile-time error: `m` is not defined on `A`
269269
obj2.m() // OK
270270
```
271-
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
272-
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,
273-
the expansion is the type of the expanded body. If the argument `b`
274-
is `true`, that type is `A`, otherwise it is `B`. Consequently, calling `meth` on `obj2`
271+
Here, the inline method `choose` returns an instance of either of the two types `A` or `B`.
272+
If `choose` had not been declared to be `transparent`, the result
273+
of its expansion would always be of type `A`, even though the computed value might be of the subtype `B`.
274+
The inline method is a "blackbox" in the sense that details of its implementation do not leak out.
275+
But if a `transparent` modifier is given, the expansion is the type of the expanded body. If the argument `b`
276+
is `true`, that type is `A`, otherwise it is `B`. Consequently, calling `m` on `obj2`
275277
type-checks since `obj2` has the same type as the expansion of `choose(false)`, which is `B`.
276278
Transparent inline methods are "whitebox" in the sense that the type
277279
of an application of such a method can be more specialized than its declared

0 commit comments

Comments
 (0)