Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Mar 15, 2020
1 parent 3832214 commit f238e74
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion docs/docs/reference/metaprogramming/inline.md
Expand Up @@ -142,6 +142,32 @@ funkyAssertEquals(computeActual(), computeExpected(), computeDelta())
// if (actual - expected).abs > computeDelta() then
// throw new AssertionError(s"difference between ${expected} and ${actual} was larger than ${computeDelta()}")
```
### Rules for Overriding

Inline methods can override other methods and can themselves be overridden by other inline methods. The rules are as follows:

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:
```scala
abstract class A {
def f(): Int
def g(): Int = f()
}
class B extends A {
inline def f() = 22
override inline def g() = f() + 11
}
val b = B()
val a: A = b
// inlined invocatons
assert(b.f() == 22)
assert(b.g() == 33)
// dynamic invocations
assert(a.f() == 22)
assert(a.g() == 33)
```
The inlined invocations and the dynamically dispatched invocations give the same results.

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.

### Relationship to @inline

Expand Down Expand Up @@ -191,7 +217,7 @@ numeric computations.

## Specializing Inline (Whitebox)

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

Expand Down

0 comments on commit f238e74

Please sign in to comment.