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
Copy file name to clipboardExpand all lines: docs/docs/reference/metaprogramming/inline.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -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