Skip to content

Commit 4d0fc00

Browse files
committed
Add docs
1 parent 3832214 commit 4d0fc00

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,44 @@ funkyAssertEquals(computeActual(), computeExpected(), computeDelta())
142142
// if (actual - expected).abs > computeDelta() then
143143
// throw new AssertionError(s"difference between ${expected} and ${actual} was larger than ${computeDelta()}")
144144
```
145+
### Rules for Overriding
146+
147+
Inline methods can override other methods and can themselves be overridden by other inline methods. The rules are as follows:
148+
149+
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.
169+
170+
2. Inline methods can override or implement normal methods, as the previous example shows. Inline methods can be overridden only by other inline methods.
171+
172+
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; a.f() // error: cannot inline f() in A.
182+
```
145183

146184
### Relationship to @inline
147185

@@ -191,7 +229,7 @@ numeric computations.
191229

192230
## Specializing Inline (Whitebox)
193231

194-
Inline methods support the ` <: T` return type syntax. This means that the return type
232+
Inline methods support the `<: T` return type syntax. This means that the return type
195233
of the inline method is going to be specialized to a more precise type upon
196234
expansion. Example:
197235

0 commit comments

Comments
 (0)