You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inline methods can override other non-inline methods. The rules are as follows:
148
148
149
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
-
abstractclassA {
152
-
deff():Int
153
-
defg():Int= f()
154
-
}
155
-
classBextendsA {
156
-
inlinedeff() =22
157
-
overrideinlinedefg() = f() +11
158
-
}
159
-
valb=B()
160
-
vala: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
+
abstractclassA {
152
+
deff():Int
153
+
defg():Int= f()
154
+
}
155
+
classBextendsA {
156
+
inlinedeff() =22
157
+
overrideinlinedefg() = f() +11
158
+
}
159
+
valb=B()
160
+
vala: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
169
170
170
2. Inline methods are effectively final.
171
171
172
172
3. Inline methods can also be abstract. Anabstract inline method can be implemented only by other inline methods. It cannot be invoked directly:
173
-
```scala
174
-
abstractclassA {
175
-
inlinedeff():Int
176
-
}
177
-
objectBextendsA {
178
-
inlinedeff():Int=22
179
-
}
180
-
B.f() // OK
181
-
vala:A=B
182
-
a.f() // error: cannot inline f() in A.
183
-
```
173
+
```scala
174
+
abstractclassA {
175
+
inlinedeff():Int
176
+
}
177
+
objectBextendsA {
178
+
inlinedeff():Int=22
179
+
}
180
+
B.f() // OK
181
+
vala:A=B
182
+
a.f() // error: cannot inline f() in A.
183
+
```
184
184
185
185
###Relationship to @inline
186
186
@@ -260,18 +260,20 @@ class B extends A {
260
260
}
261
261
262
262
transparentinlinedefchoose(b: Boolean):A=
263
-
if b thenA() elseB
263
+
if b thennewA() elsenewB()
264
264
265
265
valobj1= choose(true) // static type is A
266
266
valobj2= choose(false) // static type is B
267
267
268
268
// obj1.m() // compile-time error: `m` is not defined on `A`
269
269
obj2.m() // OK
270
270
```
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`
275
277
type-checks since `obj2` has the same type as the expansion of `choose(false)`, which is `B`.
276
278
Transparent inline methods are "whitebox" in the sense that the type
277
279
of an application of such a method can be more specialized than its declared
0 commit comments