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
+30-2Lines changed: 30 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,34 @@ 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. Example:
129
+
```scala
130
+
0xCAFEBABE:BigDecimal
131
+
```
132
+
Because the `FromDigits` companion object defines a given `FromDigits.Floating[BigDecimal]`, then there exists a given `FromDigits[BigDecimal]`. Therefore the expression above would expand to the following, given that the digits form a hex literal:
Because the given clause of `fromRadixDigits` in the call above expects a given instance of type `FromDigits.WithRadix[BigDecimal]`, and the scala library does not define one, the following error is issued:
137
+
```scala
138
+
1|0xCAFEBABE:BigDecimal
139
+
|^
140
+
|TypeBigDecimal does not have a FromDigits instance for whole numbers with radix other than 10.
141
+
```
142
+
115
143
### Example
116
144
117
145
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 +202,7 @@ With the setup of the previous section, a literal like
0 commit comments