Pre-SIP: Support Binary Integer Literals

It’d probably be easier to define a .toInt on Binary, otherwise you’d have to import the feature flag for implicit conversions anywhere you want to adapt the types (which is a pain in the neck).

Otherwise, I agree it’s probably the simplest way to differentiate them.

2 Likes

I just recently ported the hex and bin macro-based interpolators to Dotty: https://github.com/scodec/scodec-bits/blob/topic/dotty/core/shared/src/main/scala-0.22/scodec/bits/Interpolators.scala

scala> import scodec.bits._

scala> val b = hex"deadbeef"
val b: scodec.bits.ByteVector = ByteVector(4 bytes, 0xdeadbeef)

scala> val c = bin"1100110"
val c: scodec.bits.BitVector = BitVector(7 bits, 0xcc)

Per a suggestion from @soronpo, I was able to get FromDigits for ByteVector working as well: https://github.com/scodec/scodec-bits/blob/topic/dotty/core/shared/src/main/scala-0.22/scodec/bits/ByteVectorPlatform.scala

This gives usage like:

scala> import scodec.bits._

scala> val b: ByteVector = 0x00112233_44556677_8899aabb_ccddeeff_fedba098
val b: scodec.bits.ByteVector = ByteVector(20 bytes, 0x00112233445566778899aabbccddeefffedba098)

I’d love to see support for radix 2 with FromDigits via 0b prefix.

Aside: I noticed when using hex literals, the string passed to fromDigits includes a leading 0. E.g., 0xdeadbeef results in a digits string “0deadbeef”.

6 Likes