@@ -33,8 +33,8 @@ object Logger {
33
33
The ` Config ` object contains a definition of the ** inline value** ` logging ` .
34
34
This means that ` logging ` is treated as a _ constant value_ , equivalent to its
35
35
right-hand side ` false ` . The right-hand side of such an ` inline val ` must itself
36
- be a [ constant expression] ( https://scala-lang.org/files/archive/spec/2.12/06-expressions.html#constant-expressions ) . Used in this
37
- way, ` inline ` is equivalent to Java and Scala 2's ` final ` . Note that ` final ` , meaning
36
+ be a [ constant expression] ( https://scala-lang.org/files/archive/spec/2.12/06-expressions.html#constant-expressions ) .
37
+ Used in this way, ` inline ` is equivalent to Java and Scala 2's ` final ` . Note that ` final ` , meaning
38
38
_ inlined constant_ , is still supported in Dotty, but will be phased out.
39
39
40
40
The ` Logger ` object contains a definition of the ** inline method** ` log ` . This
@@ -58,7 +58,7 @@ def factorial(n: BigInt): BigInt = {
58
58
}
59
59
```
60
60
61
- If ` Config.logging == false ` , this will be rewritten (simplified) to
61
+ If ` Config.logging == false ` , this will be rewritten (simplified) to:
62
62
63
63
``` scala
64
64
def factorial (n : BigInt ): BigInt = {
@@ -120,7 +120,7 @@ inline def power(x: Double, n: Int): Double = {
120
120
}
121
121
```
122
122
123
- Parameters of inline methods can be marked ` inline ` . This means
123
+ Parameters of inline methods can have an ` inline ` modifier as well . This means
124
124
that actual arguments to these parameters must be constant expressions.
125
125
For example:
126
126
@@ -216,14 +216,14 @@ type `1`.
216
216
``` scala
217
217
inline def zero () <: Int = 0
218
218
219
- final val one : 1 = zero() + 1
219
+ val one : 1 = zero() + 1
220
220
```
221
221
222
222
## Inline Conditionals
223
223
224
- If the condition of an if-then-else expressions is a constant, the expression simplifies to
225
- the selected branch. Prefixing an if-then-else expression with ` inline ` forces
226
- the condition to be a constant, and thus guarantees that the conditional will always
224
+ If the condition of an if-then-else expressions is a constant expression then it simplifies to
225
+ the selected branch. Prefixing an if-then-else expression with ` inline ` enforces that
226
+ the condition has to be a constant expression , and thus guarantees that the conditional will always
227
227
simplify.
228
228
229
229
Example:
@@ -272,8 +272,8 @@ The scrutinee `x` is examined statically and the inline match is reduced
272
272
accordingly returning the corresponding value (with the type specialized due to
273
273
the ` <: ` in the return type). This example performs a simple type test over the
274
274
scrutinee. The type can have a richer structure like the simple ADT below.
275
- ` toInt ` matches the structure of a number in Church-encoding and _ computes _ the
276
- corresponding integer.
275
+ ` toInt ` matches the structure of a number in [ Church-encoding] ( https://en.wikipedia.org/wiki/Church_encoding )
276
+ and _ computes _ the corresponding integer.
277
277
278
278
``` scala
279
279
trait Nat
@@ -319,11 +319,11 @@ the singleton type `2`.
319
319
320
320
#### ` erasedValue `
321
321
322
- We have seen so far inline methods that take terms (tuples and integers) as
322
+ So far we have seen inline methods that take terms (tuples and integers) as
323
323
parameters. What if we want to base case distinctions on types instead? For
324
324
instance, one would like to be able to write a function ` defaultValue ` , that,
325
- given a type ` T ` returns optionally the default value of ` T ` , if it exists. In
326
- fact, we can already express this using rewrite match expressions and a simple
325
+ given a type ` T ` , returns optionally the default value of ` T ` , if it exists.
326
+ We can already express this using rewrite match expressions and a simple
327
327
helper function, ` scala.compiletime.erasedValue ` , which is defined as follows:
328
328
329
329
``` scala
@@ -333,11 +333,13 @@ erased def erasedValue[T]: T = ???
333
333
The ` erasedValue ` function _ pretends_ to return a value of its type argument
334
334
` T ` . In fact, it would always raise a ` NotImplementedError ` exception when
335
335
called. But the function can in fact never be called, since it is declared
336
- ` erased ` , so can be only used at compile-time during type checking.
336
+ ` erased ` , so can only be used at compile-time during type checking.
337
337
338
338
Using ` erasedValue ` , we can then define ` defaultValue ` as follows:
339
339
340
340
``` scala
341
+ import scala .compiletime .erasedValue
342
+
341
343
inline def defaultValue [T ] = inline erasedValue[T ] match {
342
344
case _ : Byte => Some (0 : Byte )
343
345
case _ : Char => Some (0 : Char )
@@ -360,10 +362,11 @@ Then:
360
362
val dAny : None .type = defaultValue[Any ]
361
363
```
362
364
363
- As another example, consider the type-level version of ` toNat ` above the we call
364
- ` toIntT ` : given a _ type_ representing a Peano number, return the integer _ value_
365
- corresponding to it. Consider the definitions of numbers as in the _ Inline
366
- Match_ section aboce. Here's how ` toIntT ` can be defined:
365
+ As another example, consider the type-level version of ` toInt ` below:
366
+ given a _ type_ representing a Peano number,
367
+ return the integer _ value_ corresponding to it.
368
+ Consider the definitions of numbers as in the _ Inline
369
+ Match_ section above. Here is how ` toIntT ` can be defined:
367
370
368
371
``` scala
369
372
inline def toIntT [N <: Nat ] <: Int = inline scala.compiletime.erasedValue[N ] match {
@@ -391,6 +394,8 @@ If an inline expansion results in a call `error(msgStr)` the compiler
391
394
produces an error message containing the given ` msgStr ` .
392
395
393
396
``` scala
397
+ import scala .compiletime .{error , code }
398
+
394
399
inline def fail () = {
395
400
error(" failed for a reason" )
396
401
}
403
408
inline def fail (p1 : => Any ) = {
404
409
error(code " failed on: $p1" )
405
410
}
406
- fail(indentity (" foo" )) // error: failed on: indentity ("foo")
411
+ fail(identity (" foo" )) // error: failed on: identity ("foo")
407
412
```
408
413
409
414
## Summoning Implicits Selectively
0 commit comments