Skip to content

Fix 10156: Put generic number literals under a flag #10286

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

Merged
merged 5 commits into from
Nov 16, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Nov 11, 2020

Actually, under a language import

language.experimental.genericNumberLiterals

@soronpo
Copy link
Contributor

soronpo commented Nov 11, 2020

Don't forget to mention this flag is required in https://dotty.epfl.ch/docs/reference/changed-features/numeric-literals.html

@sjrd
Copy link
Member

sjrd commented Nov 12, 2020

We should add a neg test that makes sure that the feature is not accidentally enabled if the language import is not present. I suggest the following:

import scala.util.FromDigits

object Test extends App {

  val x: BigInt = 13232202002020202020202 // error
  val z: BigDecimal = 132322020020.223 // error

  case class Even(n: Int)

  given FromDigits[Even] {
    def fromDigits(digits: String): Even = {
      val intValue = digits.toInt
      if (intValue % 2 == 0) Even(intValue)
      else throw FromDigits.MalformedNumber()
    }
  }

  val e: Even = 1234 // error

  try {
    println(123: Even) // error
  } catch {
    case ex: FromDigits.MalformedNumber => println("malformed")
  }

  x match {
    case 13_232_202_002_020_202_020_202 => () // error
  }
  (x: Any) match {
    case 13232202002020202020202: BigInt => () // error
  }
  x match {
    case 13232202002020202020202 => assert(false) // error
    case -0xaabb12345ACF12345AC => () // error
  }

  (e: Any) match {
    case 1234: Even => // error
    case _: Even => // error
  }
}

@bishabosha
Copy link
Member

bishabosha commented Nov 12, 2020

should there be a follow up to move the FromDigits class to another package/lib or is it generally useful enough to keep in the library?

@odersky
Copy link
Contributor Author

odersky commented Nov 12, 2020

should there be a follow up to move the FromDigits class to another package/lib or is it generally useful enough to keep in the library?

I had a look at it, but then decided that the downsides would be too high.

  • it's used by the compiler itself
  • it's used in the community build (scodec)

So, moving it elsewhere would cause a lot of breakage, for no apparent gain. The functionality to convert a digit string into some given type is useful.

Also it's not like generic number literals have been rejected. They have been deferred since they are non-essential for 3.0. I suspect they'll make a comeback.

Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@bishabosha bishabosha merged commit c548048 into scala:master Nov 16, 2020
@bishabosha bishabosha deleted the fix-#10156 branch November 16, 2020 11:01
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants