2 ms·
Side-note: const MIN_U32 = 0; const MAX_U32 = 2 ** 32 - 1; function u32(v) { if (v < MIN_U32 || v > MAX_U32) { throw Error(`Value out of ran
by johnisgood 2y ago
Side-note:
const MIN_U32 = 0;
const MAX_U32 = 2 ** 32 - 1;
function u32(v) {
if (v < MIN_U32 || v > MAX_U32) {
throw Error(`Value out of range for u32: ${v}`);
}
return leb128(v);
}
I love Ada, because you can do this:
subtype U32 is Interfaces.Unsigned_64 range 0 .. 2 ** 32 - 1;
or alternatively:
type U32 is mod 2 ** 32;
and then you can use attributes such as:
First : constant U32 := U32'First; -- = 0
Last : constant U32 := U32'Last; -- = 2 ** 32 - 1
Range_ : constant U32 := U32'Range; -- Range 0 .. 2**32 - 1
- jppittma 2y agoThat's kinda cool. I bet you could take that to the next step and allow arbitrary code for validation of types/arguments at compile time.
- majewsky 2y agoIf you're interested to learn more in whatever language, the relevant search term is "refinement type": https://en.wikipedia.org/wiki/Refinement_type https://en.wikipedia.org/wiki/Refinement_type
- pjmlp 2y agoAda keeps being used as example for subranges, however they exist already in Pascal and all Modula variants.
- johnisgood 2y agoI know. That said, I keep mentioning Ada because it is widely used in mission critical systems, and because it supports contracts (yes, I know, so does Eiffel), and you can do formal verification using Ada / SPARK, meaning that it could be used in place of Rust, whereas Pascal probably not.