-
Notifications
You must be signed in to change notification settings - Fork 107
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
Feedback Wanted: Explicitly type byte vectors using 0u8 #815
Conversation
Automatic search and replace, using this script: sed -i -e "s/\[0;/\[0u8;/g" `find zebra* -name '*.rs'`
I think its relatively unlikely that we'd ever create an array type that needs to be an array of bytes to avoid a bug and then never use that array in a position that lets the compiler infer the correct type. So I'm not particularly excited about this change. If everyone else wants this change I'm not opposed, I don't think it hurts anything, even readability is fine IMO, but likewise I don't think it helps much either. |
i'm having a major deja vú right now 😵 |
I accidentally'd the button 🙈 |
I don't have a very strong opinion on this, so I'm open to consensus |
I very much like this explicitness. |
As a non strong opinion haver here I'm happy to merge this to support those who do have strong opinions. |
My feeling is basically the same as @yaahc's. I'm weakly against making this change, because I think that type annotations shouldn't be used unless they're needed. Otherwise there's no way to distinguish type annotations that are meaningful from type annotations that just replicate an already-inferred type constraint.
I'd go further and say that the fact that there's no type constraint on the array means that it doesn't need to be an array of bytes. |
Basically my intuition here is, be explicit when needed, but not by default. So I wouldn't apply this change. Maybe an explicit |
I find In the Would anyone like the opposite change? |
I would favor the opposite change, but once again, not a strong opinion haver. |
It may be worth pointing out that zcashd/librustzcash code uses I don't see why as a general principle it would be undesirable to explicitly type a value whose type would have been inferred (the idea that you want the code to be maximally general doesn't really apply in this case: you rarely if ever want byte arrays to be more general, because that's significantly less efficient). I also don't see why the more verbose |
The problem is that when you do this, it's harder to distinguish which type annotations have intent and which don't, i.e., which are intended to be meaningful and which are implied from other constraints. Like comments, type annotations should be used when there is some special intent to be conveyed to the reader, not just to describe what the code is doing. |
It's always meaningful that a byte array is a byte array, right? It's much less common to use byte arrays just because you need an array of integers that happen to be in the range 0..255, than to use a byte array because it's an I/O representation. (There are no cases of the former in this PR.) |
In #809 (comment), @dconnolly suggested we use
[0u8; N]
for byte arrays, rather than[0; N]
.This change makes sure that the array is a byte array, rather than risking type inference choosing another integer size. (Or defaulting to
i32
.)However, it comes with a readability cost, so I'm looking for feedback before we merge.
This is an automatic search and replace, using this script:
sed -i -e "s/[0;/[0u8;/g"
find zebra* -name '*.rs'