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
If the condition of an if-then-else expressions is a constant, the expression simplifies to
248
+
the selected branch. Prefixing an if-then-else expression with `inline` forces
249
+
the condition to be a constant, and thus guarantees that the conditional will always
250
+
simplify.
251
+
252
+
Example:
253
+
254
+
```scala
255
+
inlinedefupdate(delta: Int) =
256
+
inlineif (delta >=0) increaseBy(delta)
257
+
else decreaseByf(delta)
258
+
```
259
+
A call `update(22)` would rewrite to `increaseBy(22`. But if `update` was called with
260
+
a value that was not a compile-time constant, we would get a compile time error like the one
261
+
below:
262
+
```
263
+
| inline if (delta >= 0) ???
264
+
| ^
265
+
| cannot reduce inline if
266
+
| its condition
267
+
| delta >= 0
268
+
| is not a constant value
269
+
| This location is in code that was inlined at ...
270
+
```
271
+
272
+
## Inline Matches
238
273
239
274
A `match` expression in the body of an `inline` method definition may be
240
275
prefixed by the `inline` modifier. If there is enough static information to
241
276
unambiguously take a branch, the expression is reduced to that branch and the
242
-
type of the result is taken. The example below defines an inline method with a
277
+
type of the result is taken. If not, a compile-time error is raised that
278
+
reports that the match cannot be reduced.
279
+
280
+
The example below defines an inline method with a
243
281
single inline match expression that picks a case based on its static type:
244
282
245
283
```scala
@@ -275,12 +313,11 @@ val intTwo: 2 = natTwo
275
313
276
314
`natTwo` is inferred to have the singleton type 2.
277
315
278
-
#### scala.compiletime._
316
+
##The scala.compiletime Package
279
317
280
-
This package contains helper definitions providing support for compile time
281
-
operations over values.
318
+
The `scala.compiletime` package contains helper definitions that provide support for compile time operations over values. They are described in the following.
282
319
283
-
##### Const Value & Const Value Opt
320
+
####`constValue`, `constValueOpt`, and the `S` combinator
284
321
285
322
`constvalue` is a function that produces the constant value represented by a
286
323
type.
@@ -302,7 +339,7 @@ enabling us to handle situations where a value is not present. Note that `S` is
302
339
the type of the successor of some singleton type. For example the type `S[1]` is
303
340
the singleton type `2`.
304
341
305
-
##### Erased Value
342
+
####`erasedValue`
306
343
307
344
We have seen so far inline methods that take terms (tuples and integers) as
308
345
parameters. What if we want to base case distinctions on types instead? For
@@ -318,7 +355,7 @@ erased def erasedValue[T]: T = ???
318
355
The `erasedValue` function _pretends_ to return a value of its type argument
319
356
`T`. In fact, it would always raise a `NotImplementedError` exception when
320
357
called. But the function can in fact never be called, since it is declared
321
-
`erased`, so can be only used a compile-time during type checking.
358
+
`erased`, so can be only used at compile-time during type checking.
322
359
323
360
Using `erasedValue`, we can then define `defaultValue` as follows:
324
361
@@ -363,19 +400,19 @@ final val two = toInt[Succ[Succ[Zero]]]
363
400
behavior. Since `toInt` performs static checks over the static type of `N` we
364
401
can safely use it to scrutinize its return type (`S[S[Z]]` in this case).
365
402
366
-
##### Error
403
+
####`error`
367
404
368
-
This package provides a compile time `error` definition with the following signature:
405
+
The `error` method is used to produce user-defined compile errors during inline expansion.
0 commit comments