Skip to content

Commit f238e74

Browse files
committed
Add docs
1 parent 3832214 commit f238e74

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ 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 also be abstract. Inline methods can be overridden or implemented only by other inline methods.
145171

146172
### Relationship to @inline
147173

@@ -191,7 +217,7 @@ numeric computations.
191217

192218
## Specializing Inline (Whitebox)
193219

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

0 commit comments

Comments
 (0)