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/changed-features/numeric-literals.md
+32-2Lines changed: 32 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ val y: BigInt = 0x123_abc_789_def_345_678_901
45
45
valz:BigDecimal=111222333444.55
46
46
```
47
47
are legal by rule (2), since both `BigInt` and `BigDecimal` have `FromDigits` instances
48
-
(which implement the `FromDigits` subclasses `FromDigits.WithRadix` and `FromDigits.Decimal`, respectively).
48
+
(which implement the `FromDigits` subclasses `FromDigits.WithRadix` and `FromDigits.Floating`, respectively).
49
49
On the other hand,
50
50
```scala
51
51
valx=-10_000_000_000
@@ -112,6 +112,36 @@ class NumberTooSmall (msg: String = "number too small") extends FromDigi
112
112
classMalformedNumber(msg: String="malformed number literal") extendsFromDigitsException(msg)
113
113
```
114
114
115
+
### Compiler Expansion
116
+
117
+
The companion object `FromDigits` also defines four methods that may be used to provide a given instance of one of the subclasses of `FromDigits`:
118
+
```scala
119
+
inlinedeffromDigits[T](givenx:FromDigits[T]): x.type= x
120
+
121
+
inlinedeffromRadixDigits[T](givenx:FromDigits.WithRadix[T]): x.type= x
122
+
123
+
inlinedeffromDecimalDigits[T](givenx:FromDigits.Decimal[T]): x.type= x
124
+
125
+
inlinedeffromFloatingDigits[T](givenx:FromDigits.Floating[T]): x.type= x
126
+
```
127
+
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.
129
+
130
+
As an example, the literal below has a nonsensical expected type `BigDecimal`, which can not be constructed with hex digits:
131
+
```scala
132
+
0xCAFEBABE:BigDecimal
133
+
```
134
+
Upon the compiler finding a given instance for `FromDigits[BigDecimal]`, the hex literal above expands to the following:
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:
139
+
```scala
140
+
1|0xCAFEBABE:BigDecimal
141
+
|^
142
+
|TypeBigDecimal does not have a FromDigits instance for whole numbers with radix other than 10.
143
+
```
144
+
115
145
### Example
116
146
117
147
As a fully worked out example, here is an implementation of a new numeric class, `BigFloat`, that accepts numeric literals. `BigFloat` is defined in terms of a `BigInt` mantissa and an `Int` exponent:
@@ -174,7 +204,7 @@ With the setup of the previous section, a literal like
0 commit comments