-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Favour error over implicit conversions with generic number literals #7468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Favour error over implicit conversions with generic number literals #7468
Conversation
93a44dc
to
8f2ee3d
Compare
2ffab02
to
2819278
Compare
d739b33
to
af43f16
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR should also update the doc page to reflect what is implemented.
ea1e00a
to
7890726
Compare
I've updated the docs |
@@ -112,6 +112,36 @@ class NumberTooSmall (msg: String = "number too small") extends FromDigi | |||
class MalformedNumber(msg: String = "malformed number literal") extends FromDigitsException(msg) | |||
``` | |||
|
|||
### Compiler Expansion | |||
|
|||
The companion object `FromDigits` also defines four methods that may be used to provide a given instance of one of the subclasses of `FromDigits`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is not enough. For instance, it is said that the companion object defines a method fromDecimalDigits
but it is not specified where and when this method is called.
I believe if we want to this to go ahead, it needs a complete rework of the doc page, and the SIP committee has to decide on the changes. My concern is that once the additions are fully worked in, we might decide that things became too complex.
One possibility is to put this on hold until the SIP committee has had a chance to go over it. |
I've updated the doc page to list when each summoner method is used, as a stand in until rewriting. |
I've update the documentation again, I believe that it is easier to read, and it also incorporates the motivation for this change as a safety feature. |
725f436
to
4155cf2
Compare
I do not see it as a problem if numeric literals get converted to whatever possible type by implicit conversion. On the contrary, I think it would be a nice feature if the system was re-implemented in terms of implicit conversion. A numeric literal would be encoded as some hidden type (corresponding to FromDigits and subtraits as suited) and there would be an implicit conversion from the hidden type to the target type. This would allow numeric literals to take part in any chain of implicit conversions one could imagine, for instance in cases like:
Edit : implicit conversion from literal to BigDecimal would make implicit conversion from Int to BigDecimal unnecessary. Consequently we could dispose of it, and the problematic example |
Closing for now. To be revived when we come to to generic number literals. |
label t:on-hold? |
Currently, if we do something like
val x = 0xcafebabe: BigDecimal
, then we get aBigDecimal
after some implicit conversion from anInt
, because there is no instance ofFromDigits.WithRadix[BigDecimal]
, as decimals only work with base 10. Someone may find this surprising when using the explicit type ascription to implement a custom number type.FromDigits[T]
is of the correct subclass, (WithRadix
,Decimal
orFloating
) else issue a compile time error.T
and then callingfromDigits
on the result, which will cause a compile time error if theFromDigits[T]
is of the incorrect subtype, rather than silently dropping to the defaultInt
orDouble
interpretation.Alternatives
FromDigits[T]
, this relies on macro subtype-checks to get a precise type, so does not appear in the signature - this allows a custom error message without synthesising a temporary given if one does not exist.implicitNotFound
messages to the subtypes ofFromDigits
and calling the summoners after proving there is already a given instance in scope of justFromDigits[T]
. This would mean library code users would have to synthesise a given to get the same error.