-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support hexadecimal and octal syntax for integers #7578
Comments
Hello, I would love to work on a this ( ˶˃ᆺ˂˶) The implementation of |
The point of this issue is to add native literals to the language. I'm sure Nixpkgs can implement The goal here is to have the following. # this is a nix file
assert 15 == 0xf;
assert toString 0xf == "15";
assert 0o10 == 8;
# etc You may notice that github doesn't know how to highlight that; Nix doesn't know how to parse it either, yet. |
Oh no! I forgot about the list item separator... This proposal does steal usable syntax:
It's still unlikely to cause a problem, but it does mean that this is technically a breaking change. |
Might I recommend that instead of
This parses easily because it still begins with a numeral. If this was implemented with case insensitivity, you get 36 bases. |
Why? Is there a use case for making base 23 or base 37 easier to write? |
This seems over-engineered, and the base 32 wouldn't match Nix's own base32 encoding; lack of base 64 would be surprising. I'd like to reserve
|
We might also want to steal syntax such as |
Nope - but it's all just the modulo operator, so why go look for hardcoded chars when we could take the I would argue that it's simpler/easier with Time (bases 24 & 60) would also be somewhat realistic use-case(s) (since you asked), for a vast majority of us using Nix.
Re. over-engineering; see above. Re. base64; this suggestion would make the case for bases higher than 36 simpler to write/extend once someone(tm) figures out how to solve the (completely orthogonal to this discussion - at least so far) problem with base64.
Good enough argument for me to move away from EDIT: Minor edits for clarity/readability. |
Decimal is a given, hex is common, octal is somewhat niche and binary is quite niche. I don't imagine getting requests for other bases in this fashion. These prefixes are also standard across the computing world, no need to have some bespoke integer format.
This is not how you represent time, so that's actually hurting your point. |
I am not as confident as you in your (I feel too) strong assertion(s).
I definitely think your argument is over-reaching here - seems (to me) like you're presuming knowledge of all use-cases. |
Cherry picking? This is already a thing in Bash but it uses $ echo $((23#55))
I presume you mean the SHA sums for packages. This is semantically different, people are not expected to read hashes as actual numbers, or actually reason about them. Base 64 has always been a tricky one. I would love for a way to do it for base 64.
I'm fine with the 'b' character. I think 'n' could be good too.
I'm also of the opinion that we could just have a builtin called
|
I have no objection to a builtin for less-used bases. All I'm getting at is I don't think that should block movement on this feature request. Nor do I think we should look to bash for user friendliness. |
Arbitrary base literals are hard to read and understand, and any remaining use cases are sufficiently niche that they're better covered by a library function.
This is complete gibberish to me. I do have an objection to a new built-in. Builtins have to be absolutely bug-free and can not be changed. Furthermore they make the implementation of alternate evaluators needlessly hard. Niche functions like arbitrary-base parsing are better implemented in a Nix-language library such as a flake, which does not suffer from these problems, or |
I'm tapping out of this back and forth. But it's important to me to clarify for anyone who cares that none of my inputs to this thread was meant as @eclairevoyant interpreted them;
|
fair enough, just thought I would pitch in. |
Do you still feel that's the case with the below code? I'm curious. let
dayAndAHalf = 12n3;
in {
intervalAttribute = 3 * dayAndAHalf;
} |
12n3 is gibberish as well. What does that have to do with a day and a half?
Of course this discussion is blocking since it directly conflicts with the proposed syntax. If it wasn't blocking then it should be a separate request, yes? |
Did you mean |
Not engaging.
I'm on the same page.
Then where are discussions to be held? |
I needed octal support to represent file permissions in a module today. Converting the octal int from its string representation into a decimal integer was the only safe way I found to transport it through PyYAML (wants octal numbers to be zero-prefixed) into Python's Anyway, since this was one of the first issues I found on the matter: Here is a function that converts an octal string into an integer with base 8. toIntBase8 = str:
lib.pipe str [
lib.stringToCharacters
(map lib.toInt)
(lib.foldl (acc: digit: acc * 8 + digit) 0)
]; |
|
Is your feature request related to a problem? Please describe.
Some configuration formats support hex or octal syntax. It would be nice if those numbers were easily expressed in Nix as well.
Describe the solution you'd like
Extend the parser to parse
0x[:xdigit]+
as a integer converted from base 16Currently e.g.
0xff
is parsed as0 xff
, whichwould never return anythingis only valid syntax when used inside a list literal.We can assume that all expressions containing a hexadecimal integer are currently invalid expressions. That makes this change an improvement instead of a breaking change.Nonetheless, this seems like a very unlikely sub-expression in practice, as most people have used actual whitespace to separate numbers and identifiers in lists, if they even consider building a heterogeneous list.Similarly, extend the parser to parse
0o[0-7]+
as a integer converted from base 8.Similarly, make sure negative integers in these bases work.
I would suggest not to save the base of the integer in its
Value
representation. This keeps integers simple.Describe alternatives you've considered
Add base 8 or 16 conversion functions to the Nixpkgs lib. The end user syntax would not be great, for example:
lib.fromHex "0xff"
.Saving the base of the integer in its
Value
representation might be useful for detecting decimal integers used in places where an octal integer is expected. If the user writes an octal integer using decimal syntax, we could detect that mistake. However, I don't think it's currently worth complicating the meaning of integers for this.Additional context
Inspired by NixOS/nixpkgs#208747 (comment)
Priorities
Add 👍 to issues you find important.
The text was updated successfully, but these errors were encountered: