4 ms·
Why not just go to values? Some languages like Ada allow a type that say goes from -273 to 600.
by m463 7y ago
Why not just go to values?
Some languages like Ada allow a type that say goes from -273 to 600.
- Gibbon1 7y agoI would very much prefer this and the ability to spec what happens on overflow. There are a lot of arguments back and forth because there actually is no 'right way' to handle overflow.
- guerby 7y agoAnd GNAT (GCC Ada front-end) will use a biased representation for range type when packing tight: with Ada.Text_IO; use Ada.Text_IO; procedure T is type T1 is range 16..19; type T2 is range -7..0; type R is record A : T1; B,C : T2; end record; for R use record A at 0 range 0 .. 1; B at 0 range 2 .. 4; C at 0 range 5 .. 7; end record; X : R := (17,-2,-3); begin Put_Line(X'Size'Image); -- 8 bits end T;
- weinzierl 7y agoPascal has it too. I always found it quite natural to specify the range I want with the data type and not as precondition in the function. Another big advantage is that you can avoid a good deal of potential off-by-one errors, if you define your data types appropriately. For example the following definition in Pascal would be much less error prone than the corresponding definition in C[1]: var weekday: 0 ... 6; monthday: 1 ... 31; For a quarter of a century I wonder why no one seems to miss that feature. I really hope we will get them in C one day. Even more so I hope that the proposals for refinement types in Rust[2] will one day be resolved and become implemented. [1] https://www.cplusplus.com/reference/ctime/tm/ https://www.cplusplus.com/reference/ctime/tm/ [2] https://github.com/rust-lang/rfcs/issues/671 https://github.com/rust-lang/rfcs/issues/671