Skip to content

Commit 181e450

Browse files
committed
fully document usage of FromDigits summoners.
1 parent 0a720d8 commit 181e450

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

docs/docs/reference/changed-features/numeric-literals.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,26 @@ inline def fromDecimalDigits[T](given x: FromDigits.Decimal[T]): x.type = x
125125
inline def fromFloatingDigits[T](given x: FromDigits.Floating[T]): x.type = x
126126
```
127127

128-
If a numeric literal has a known expected type `T` that is not one of the primitive numeric types, then the compiler will search for a given instance of `FromDigits[T]`. If one exists, then the compiler expands the literal to a call on the `fromDigits` method on the result obtained from calling one of the above four methods.
128+
If a numeric literal has a concrete expected type `T` that is not one of the primitive numeric types, the compiler will search for a given instance of `FromDigits[T]`. If one exists, then the compiler will replace the numeric literal with an application of its digits to the `fromDigits` method on the result of the application of `T` to one of the above four methods, resulting in the following:
129+
130+
- `fromDigits[T].fromDigits(digits)` if the literal forms a whole number with base-10.
131+
- `fromRadixDigits[T].fromDigits(digits, 16)` if the literal forms a whole number with base-16.
132+
- `fromDecimalDigits[T].fromDigits(digits)` if the literal is base-10 with a single decimal point.
133+
- `fromFloatingDigits[T].fromDigits(digits)` if the literal is base-10 with an exponent and optionally a single decimal point.
134+
135+
<!-- on the `fromDigits` method on the result obtained from calling one of the -->
129136

130137
As an example, the literal below has a nonsensical expected type `BigDecimal`, which can not be constructed with hex digits:
131138
```scala
132-
0xCAFEBABE: BigDecimal
139+
0xABC_123: BigDecimal
133140
```
134-
Upon the compiler finding a given instance for `FromDigits[BigDecimal]`, the hex literal above expands to the following:
141+
Upon the compiler finding a given instance for `FromDigits[BigDecimal]`, the hex literal above expands to the following, after removing numeric separators:
135142
```scala
136-
FromDigits.fromRadixDigits[BigDecimal].fromDigits("0CAFEBABE", 16)
143+
FromDigits.fromRadixDigits[BigDecimal].fromDigits("0ABC123", 16)
137144
```
138145
The given clause of `fromRadixDigits` asserts that the prior found `FromDigits` instance is a subtype of `FromDigits.WithRadix[BigDecimal]`, or else following error is issued:
139146
```scala
140-
1 |0xCAFEBABE: BigDecimal
147+
1 |0xABC_123: BigDecimal
141148
|^
142149
|Type BigDecimal does not have a FromDigits instance for whole numbers with radix other than 10.
143150
```

0 commit comments

Comments
 (0)