Repository navigation
Description
I will attempt to summarise here the primary points of discussion that have ensued in #7993 as it has spiraled into many threads, and I suspect a bullet-point summary of questions to be answered will be significantly easier on the committee.
Regarding Binary & Hex Parsing
As part of the refactor & introduction of binary parsing, methodology of hex parsing has also been altered a bit. Parsing currently results in the same as it currently works, with the caveat that literals with values above Int64.MaxValue are also now acceptable.
Parse as C# Literals
With that in mind, mklement brought up the point that we may want to simply change how hex and binary parsing work. That is, mimic C#'s behaviour of these literals in source, which would mean parsing all hexadecimal literals as strictly positive (no more 0xFFFFFFFF -eq -1 — instead, 0xFFFFFFFF -eq UInt32.MaxValue) and having such literals smoothly convert up to UInt values.
With that in mind, the code patterns for hex or binary literals would seek out the lowest available parsed value type (when no type is specified) in the following order: Int32, UInt32, Int64, UInt64, Decimal, and possibly finally BigInteger.
Other Options
Hex & BigInteger
If we elect to keep current hex behaviour, we need to consider how it would behave in ranges higher than Decimal. BigInteger's default parser for hex numerals will simply assume the highest bit of a byte is indicative of the sign. As a result any numeral treated as signed that begins with 0x8 or higher will be considered the negative two's complement representation when we enter ranges that can only be parsed as BigInteger. This could be overridden easily, if this behaviour is considered to be undesirable.
Binary Parsing with Sign Bits
Then we face the issue of what to do about binary parsing. I doubt most folks working with binary directly will be working in ranges above 32-bit numbers, but I could be very wrong on that. They are, however, easier to work with in the byte, short, and int value ranges (8, 16, 32-length literals), and behaviour of a sign bit in this case is also entirely up to the parser here due to the custom implementation of binary parsing for speed concerns.
Should binary sign bits only be accepted at 32-bit lengths and up for consistency with hex parsing? Or should they be accepted at similar lengths of literal (8 binary bits, 8 hex char literal) to match up visually with hex literals? This would place a sign bit at all of the 8, 16, and 32-char lengths of a binary literal, so 0b11111111 -eq -1 and so forth, which looks similar in behaviour to hex's 0xFFFFFFFF -eq -1, despite the obvious difference in actual bit length of the literals.
Parse Numeric Literals with Underscores
E.g., 1_000_000, 0b0101_1010, 0xF_FFF_F_FFF and so forth. Should this be allowed? C# already does this with literals in source code. Are there culture-sensitive concerns around this? This is a relatively simple addition.
Experimental Feature Possibilities
If this is the best option, I am not at all against hiding alternate parse methods behind experimental flags if need be. But for that to be possible, I need a "standard" acceptable behaviour to be defined clearly so that I can lay it out for the hex and binary parse methods.
Original post is below. PR #7901 added byte literals (suffix y or uy), so that portion of this issue is completed.
See the discussion in #7509.
Emerging from the interesting dust of modifying the tokenizer are two further points:
- The tokenizer should be able to parse binary literals.
- The tokenizer should support
bytetype literals.
The trouble here is that both of these suggestions could arguably use a b suffix for numeric literals.
My opinion is that the b suffix should be used for byte literals, in keeping with the current convention that suffixes alter the numeric data type rather than simply base representation.
So what about binary? Well, jcotton in the PowerShell Slack channel / irc / discord #bridge channel mentioned that just like common hex representations use 0xAAFF057, we could also follow the common convention of binary being similar: 0b01001011
From my brief poking about, it looks like we may have to alter System.Globalization.NumberStyles in order to add Binary as a flag value -- if we follow the current implementation of hexadecimal numerals. We don't necessarily have to.
TryGetNumberValue in the tokenizer.cs file would also have to be modified to accept possibly some kind of enum for number formats as well; currently it only accepts a bool for hex specification. ScanNumberHelper would also have to be modified for this.
The suffix approach is simpler, especially with the changes already in #7509 which make adding suffixes a good deal easier. However, given that we may want to reserve the b suffix for 123b byte literals, we may need to consider adding a case for 0b0010101 syntax.
What do you guys think?
Other suggested suffixes for byte literals:
ub(sborbfor signed bytes)uy( F# style) withyfor signed bytes
Activity
rjmholt commentedon Aug 17, 2018
+1 on:
bsuffix is for bytes0bprefix is for binaryMy only problem is that the most convenient way to specify a byte will be in hex, and
0xffbcan't specify a byte because its a valid ordinary hex literal.It might not be possible to accomplish nicely, but my ideal would be able to specify a byte literal with hex.
vexx32 commentedon Aug 17, 2018
We could adopt the
uysuffix like F# for bytes, in that case?iSazonov commentedon Aug 17, 2018
Could we use 'ub' for bytes and 'b' for binaries.
The use of the '0b' prefix does not seem to be consistent.
vexx32 commentedon Aug 17, 2018
we could use anything we want, in fact. Just gotta code for it in the tokenizer... and I've had plenty of experience doing that recently :)
jcotton42 commentedon Aug 17, 2018
Ilya (@iSazonov) every language I've run across uses an
0bprefix for binary numbers. Plus using a prefix to change the base is consistent with0xfor hex.vexx32 commentedon Aug 17, 2018
If we take the F# route they also have
yforsbytewhich... I don't really know how useful it is, but it's a possibility.With this sort of syntax your literals would look like:
iSazonov commentedon Aug 17, 2018
We usually consider C# syntax first. In C# 7 we get '0b' for binary literals but there is still not exist a suffix for byte. I think we should try to find a discussion in .Net repos about the suffix - I guess they already have it. If the discussion is still not finished we have to postpone the decision on the suffix to keep a consistency with C# in future.
For binary we could support a format
var b = 0b1010_1011_1100_1101_1110_1111;vexx32 commentedon Aug 17, 2018
The link to Roslyn here seems to be defunct as they shifted to Github, but nonetheless the quote seems to indicate:
https://stackoverflow.com/questions/5378036/literal-suffix-for-byte-in-net
They went for zero ambiguity and decided
sbfor signed andubfor unsigned (since .NET bytes are default unsigned, but everything else is usually signed as standard).iSazonov commentedon Aug 17, 2018
I think it is here dotnet/csharplang#1058 without any progress.
vexx32 commentedon Aug 17, 2018
For me, the suffix
bin general might be a bit lacklustre forbytesimply because to a new user it could easily be read asbit, orbinaryinstead and be confused. At least withyif they don't know they're probably more likely to actually look it up rather than guessing and being burned by it.But I guess if we want to stick with parity we might want to stick with the most likely candidates for inclusion in C#.
It's also a non-zero possibility that implementing this in PS may affect the discussion of inclusion in C# as it remains open after almost a year.
iSazonov commentedon Aug 17, 2018
I commented in csharplang repo.
rjmholt commentedon Aug 17, 2018
Also want to note that underscore syntax you used there Ilya (@iSazonov). I was thinking about the same thing the other night. I think it would be worth implementing underscore ignoring in numeric literals too (separate issue I know, but thought I'd raise it here first in case other people think it's stupid). It looks like C# 7, Java 7 and OCaml have this already, and it would certainly make sense alongside a binary literal syntax.
42 remaining items
vexx32 commentedon Oct 7, 2018
Thanks Steve!
HumanEquivalentUnit and I have discovered a new
BigIntegerconstructor introduced in .NET Core 2.0 and have been toying with parsing binary using it:Benchmarks of a bunch of different methods are looking very good indeed. Some of the slower possible methods we've come up with (he's been doing a lot of the tinkering here) are still about twice as fast as
Convert.ToIntX()methods, even with smaller numbers. 😄Beyond that, we're subdividing nanoseconds finding quicker methods, so I think I can call that "good enough" for the forseeable future!
iSazonov commentedon Oct 12, 2018
Steve Lee (@SteveL-MSFT) We need PowerShell Committee conclusion to continue, please.
mklement0 commentedon Oct 22, 2018
Re use of only uppercase
Ifor[bigint], quoting #7993 (comment)While lowercase
ican indeed get confusing, I suggest not introducing an inconsistency by making theIsuffix the lone exception in terms of case-sensitivity.While it makes sense to document and recommend the use of uppercase
I, and to use it in examples, I suggest acceptingitoo, for symmetry with all other type-suffix characters.Alternatively, we could pick a different character - has
nbeen considered? (It is pretty much the only other letter left that doesn't cause outright confusion; any letter we choose is technically a breaking change in argument mode).vexx32 commentedon Oct 22, 2018
nis a pretty decent alternative... I'd rather not cause confusion for those more mathematically inclined, I think. Worth considering also, definitely.(And since it's trivially easy to implement complex numeral parsing, I'd rather not completely block that out as a possibility by taking
ias a suffix here, although in our target userbase I doubt there's a lot of use for it.)[-]Allow binary numbers and/or byte type literals[/-][+]Allow binary numbers, byte literals, and BigInteger?[/+]vexx32 commentedon Nov 15, 2018
Steve Lee (@SteveL-MSFT) Michael Klement (@mklement0) Ilya (@iSazonov)
I have attempted to summarize in the original issue description the main discussion points from #7993 as best I can to assist with committee review of the primary sticking points on that PR. 😄
SteveL-MSFT commentedon Nov 15, 2018
Rain Sallow (/u/ta11ow) (@vexx32) Thanks for the summary, this will help the review and come to a conclusion more quickly!
SteveL-MSFT commentedon Nov 28, 2018
@PowerShell/powershell-committee reviewed this, regarding the underscore, we do not accept adding that as it can cause issues with existing usage as Jason Shirk (@lzybkr) pointed out. Also, we do not want auto coercion from long to bigint as not only is this a breaking change, but may cause surprising effects to users.
Add Binary Parsing Support & Refactor TryGetNumberValue & ScanNumberH…
Add Binary Parsing Support & Refactor TryGetNumberValue & ScanNumberH…