2 ms·
From my training in other languages, I really want to use uint on values that have no meaning when negative. If you look at that statement with C eyes, that pro
by jerf 12y ago
From my training in other languages, I really want to use uint on values that have no meaning when negative. If you look at that statement with C eyes, that probably sounds insane, but if you look at it from a modern type system perspective, it sounds eminently sensible and if anything not paranoid enough (where's your upper bound?).
Unfortunately it's too darned easy to casually subtract something and bam, Big Numbers due to underflow. What I want is to be told I have bug... what I get is just the bug.
I've also encountered it in some network code where the numbers in question were bitpacked. I mean, sure, it's my bug, but philosophically I no longer think that's the end of the story. Anything a compiler or runtime can do to help me avoid bugs, it should, and noticing a truncation is definitely in that class. Again, that statement may sound crazy in a C world, but it's perfectly sensible in a modern one.
You don't get truncation bugs because you're so used to not getting any help that you just do bad things like not correctly specify your type, because you've thoroughly internalized how bad and wrong the compiler and runtime are here. You shouldn't have to lie about your types to make your code work.
- JoeAltmaier 12y agoGoes further than that, never mind the types. When in doubt I read the ASM code, check the math by hand to make sure the right opcodes got used etc. Probably for the reason you said, in C its terribly hard to tell when its going to promote a type and whether an intermediate result will be signed or not.
- TheLoneWolfling 12y agoPersonally, I really wish numeric types supported more than just unsigned/signed <minimum bit length>. Generic range types at a minimum, perhaps even "a type is defined by a pure function that returns true/false for if the value is a valid instance of that type" - want to specify that the input must be a Gaussian prime at compile time? Sure! (Or, for a saner example, want to specify that the input must be a normalized quaternion? Sure!) For generic range types, you can just specify "this stores a number in this range" (with negative infinity/infinity being valid values for the bounds) and let the compiler figure out the best way to store it (with overrides, of course. Just because you have sane defaults doesn't mean you should make people fight the language). Especially if you could specify wrapping rules. Exception on under/overflow / wrapping / saturation / "won't happen" (read: compile-time error if the compiler can show that it can overflow/underflow) - exception being the default.