Skip to content

Commit 68ed7a3

Browse files
Merge pull request #8235 from robstoll/patch-19
doc(inline): improve recursive example
2 parents e8e8bcd + 7cbbd8f commit 68ed7a3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,26 @@ power(expr, 10)
121121
```
122122

123123
Parameters of inline methods can have an `inline` modifier as well. This means
124-
that actual arguments to these parameters will be inlined in the body of the `inline def`.
125-
`inline` parameter have call semantics equivalent to by-name parameters but allows for duplication
126-
of the code in the argument. It is usualy useful constant values need to be propagated to allow
127-
further optimizations/reductions.
124+
that actual arguments to these parameters will be inlined in the body of the
125+
`inline def`. `inline` parameters have call semantics equivalent to by-name parameters
126+
but allow for duplication of the code in the argument. It is usually useful when constant
127+
values need to be propagated to allow further optimizations/reductions.
128128

129129
The following example shows the difference in translation between by-value, by-name and `inline`
130130
parameters:
131131

132132
```scala
133-
inline def sumTwice(a: Int, b: =>Int, inline c: Int) = a + a + b + b + c + c
133+
inline def funkyAssertEquals(actual: Double, expected: =>Double, inline delta: Double): Unit =
134+
if (actual - expected).abs > delta then
135+
throw new AssertionError(s"difference between ${expected} and ${actual} was larger than ${delta}")
134136

135-
sumTwice(f(), g(), h())
137+
funkyAssertEquals(computeActual(), f() + g(), h() + i() - j())
136138
// translates to
137139
//
138-
// val a = f()
139-
// def b = g()
140-
// a + a + b + b + h() + h()
140+
// val actual = computeActual()
141+
// def expected = f() + g()
142+
// if (actual - expected).abs > h() + i() - j() then
143+
// throw new AssertionError(s"difference between ${expected} and ${actual} was larger than ${h() + i() - j()}")
141144
```
142145

143146
### Relationship to @inline

0 commit comments

Comments
 (0)