News for hackers. Stuff that matters.
Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Thursday December 30, 2021 @03:26AM (#29734834)
Hmm. This means we'd have to do mod every time we accessed an array which seems not great, right? But probably it could be optimized away for the common/easy cases.
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Thursday December 30, 2021 @05:47AM (#29735598)
In WUFFS if you've got a 40 item array of things, an unsigned 8-bit integer n and you want to access things[n] the compiler will conclude n's type must be base.u8[0..40]

Now, having refined the type this way, when you try elsewhere in the code to read some unknown byte into n, WUFFS will reject that because who knows if it exceeds 40? To make the code compile, you need to actually write code that ensures n is always less than 40.

Of course you aren't going to write big general purpose software under this sort of constraint, it's like having a two year old following you everywhere insisting on a detailed explanation of why you are doing every single thing and asking the most irritating "But why?" questions after each explanation, exhausting. But if you're processing untrusted data it's exactly what you needed to stay safe.

Re:Python, unlike C, has the mod operator always return a po... by dmurray on Thursday December 30, 2021 @04:45AM (#29735218)
Julia already has bounds checking at runtime on array access, so you could do the mod operation only if the bounds check fails.

But I don't get how this would practically work. Would each array have a preferred n that is used for the mod operation? For most situations where that makes sense, using a 2d array seems more logical.

Re:Python, unlike C, has the mod operator always return a po... by half-kh-hacker on Thursday December 30, 2021 @05:16AM (#29735405)
I assume n would be size(a)
Re:Python, unlike C, has the mod operator always return a po... by dmurray on Thursday December 30, 2021 @05:28AM (#29735492)
Oh yeah - I got mixed up on this and thought we wanted to access sub arrays within a.
Re:Python, unlike C, has the mod operator always return a po... by half-kh-hacker on Thursday December 30, 2021 @05:15AM (#29735398)
The parent comment has brace-syntax as well as the square brackets.

I assume the braces are there for "I want to skip doing the wrapping"

Re:Python, unlike C, has the mod operator always return a po... by bee_rider on Thursday December 30, 2021 @09:44AM (#29737064)
I was thinking that, but they didn't mention it, so I assumed it was just a typo. Could be, though.