8 ms·
> How many times did you write "int" when you actually only wanted a positive number? subtracting one from zero and getting max_uint can be its own brand of fu
by lamontcg 5y ago
> How many times did you write "int" when you actually only wanted a positive number?
subtracting one from zero and getting max_uint can be its own brand of fucking horribly awful.
having the language itself throw in that circumstance can also be exactly what you don't want.
- Jtsummers 5y agoIn some languages you can get a choice. Broken record time, but Ada: type Byte is unsigned 2**3; This will permit any value in the range [0,7] and when you exceed it (in either direction) it will wrap around (the desired action if you choose this type). In contrast: type Byte is range 0..7; Will give you a runtime error when you exceed the bounds by trying to increase beyond 7 or decrease below 0. Having this choice is nice, you get to decide the semantics for your system.
- still_grokking 5y agoAre that static or runtime checks (or both)?
- Jtsummers 5y agoDefinitely runtime, potential for compile time. The compile time checks will at least prevent obvious cases (assigning a value out of the range like using either Byte type above: T := -1 will get a compile time error). Using the second Byte type, paired with SPARK/Ada, this bit of code should set off the proof system and prevent compilation: procedure Foo is type Byte is range 0..7; T : Byte := 0; begin T := Byte - 1; end Foo; (Not that that's useful code, but a basic example.) That shouldn't make it past the SPARK/Ada system to compilation. Now change it to this: function Foo(T : Byte) return Byte is begin return T + 1; end Foo; and SPARK/Ada should warn (been a bit, but should also fail to compile) that this could cause overflow.
- lamontcg 5y agoYeah I took Ada for CS 210/211 back in 1991, are we there yet?
- Jtsummers 5y agoAda's good, but it will never win. The open source compilers are solid, but no one wants to learn it. It's (depending on who you ask): Too old, too verbose, BDSM programming, not Rust, not Haskell, not C. It has a lot of positive features going for it, but it is verbose. That verbosity is a major distraction for people who can't handle it, they want their very short keywords or, better, no keywords just symbols. Curly braces are somehow better than begin/end even though begin/end really aren't hard to type. Ada shines, particularly, in the long tail of system maintenance, not in the writing (arguable: the type system certainly helps a lot in the writing, the syntax doesn't). So I press for it where it belongs, and don't where it doesn't. But when someone laments the state of type systems, I point it out.
- still_grokking 5y agoGive it a new syntax, and try again… It's not so difficult nowadays to write a "syntax-transpiler". The hard part, the language, would remain the same. People seem having done so successfully with OCaml, see ReScript.
- Jtsummers 5y agoThe verbosity can only be removed at a superficial level. Many people object to begin and end, but they’d chafe even more if they got further. For good reasons (Ada leans toward explicit declarations in favor of implicit ones) you have to list each sub program you depend on in a module by name. Even if you don’t import it’s symbols. And you have to explicitly instantiate generics (like C++ templates) rather than the compiler inferring your intention. Things like that cannot be removed from the language so only a superficial improvement can be achieved, for what benefit?
- still_grokking 5y ago
- klyrs 5y agoNah, unsigned ints are magical. Why check (i < 0) || (i > n) when you can just simplify it to (i > n) after verifying that n < max_uint?
- ragnese 5y ago> subtracting one from zero and getting max_uint can be its own brand of fucking horribly awful. Agreed. Silent wrap-around on overflow is also a really terrible idea. > having the language itself throw in that circumstance can also be exactly what you don't want. Then you have to check before you do it. Or call a special overflowing function. The default should absolutely be a crash. That wouldn't even really be anything new: what happens when you divide by zero in most languages? Index an array with an index > length? Should the array index just wrap around and give you the element at length % index?