Support binary integer literals. For example, the binary literal 0b00101010 would have the integer value 42.
Motivation
Several other major languages support binary integer literals, including Java (as of Java 7), Python and Rust.
Interactions with Other Language Features
Like other integer literals, binary integer literals support separators (_), which can greatly enhance the readability of larger values (e.g. 0b_1110_1101_1011_0111).
Implementation
The implementation of binary integer literals is quite simple, and can be found at:
Byte literals would also be neat. Somewhat orthogonal to this I suppose, but close enough to mention here. Do you have any appetite to bolt that on to this one Dale?
It does not seem at all controversial to me, and I suspect that others feel the same. Perhaps it could be brought up extremely briefly, and if it does not have any dissent or further discussion wanted by anyone on the committee, it can be moved through. If someone does want to discuss something about it or doesnāt like it, the discussion can be tabled for after 3.0.
It just seems a bit silly to me to table such a trivial change for such a long time if thereās so little to discuss about it, and both the spec and compiler work are already complete.
I think they are not close enough to group. There is not (to my knowledge) any syntax that already has consensus (as 0b does) and fits neatly with the rest of the Scala syntax (e.g. a Rust-like i8 does not fit well with Intās lack of suffix and Longās L suffix). Additionally, there is no working implementation for it.
If you have a concrete proposal and an implementation, I do think it might be reasonable to tackle them together, but as it does not, I donāt think it adds anything to this proposal.
I was only joking when I suggested that if we support 0x_42 in Scala 2, people will lose interest in Scala 3. But itās quite possible no one will have the resources to port their tables of binary constants to Scala 3 interpolator macros, that is, after they learn how to write interpolator macros.
However, since it takes about a year to merge a PR that corrects the caret in parse error messages, I think itās reasonable for an actual feature to require in the 3-5 year range.
Some people boast how theyāre moving the needle. Iām just trying to move the caret.
Of course, 0b is the Byte value zero. But we should deprecate that straight off and recommend 0B because itās too easy to accidentally add a digit 0b1 and wind up with an Int. Thatās probably more of a hazard in emacs than vi.
I donāt mean to make it into a meme, but could this be implemented with a macro-backed string interpolator? That way it doesnāt add to the language/compiler, and the niche users that suffer this absence gain an improvement?
How does an opaque type of Int sound? You could even give it a Conversion[Binary, Int] instance, and you could even put that in the companion object so itās always available for type adaptationā¦