9 ms·
I would much rather see most of this be generalized to a parametrized bounded integer type, e.g. int<lowerbound, upperbound>.
by amluto 4y ago
I would much rather see most of this be generalized to a parametrized bounded integer type, e.g. int<lowerbound, upperbound>.
- astrange 4y agoThis would be a big improvement over how a lot of languages handle types - namely it’d let you stop calling “i16” “i32” types when they’re actually storage sizes. “Safe” languages like Java try to achieve safety by making you write boilerplate casts here - “short = int + int” is illegal without a (short) cast even though that doesn’t change the meaning of the program. If it was “x = i32<0,3> + i32<0,3>” then it highlights how silly this is and maybe they wouldn’t make you write a (i16<0,6>) cast.
- IAmLiterallyAB 4y agoExactly. In fact, I would go far as to make this the _only_ integer type. u16, u32 etc can just be typedefs. The ability to be precise with integer ranges could prevent many types of arithmetic and OOB errors.
- tialaramex 4y agoWUFFS types can be refined as well as having a native representation so e.g. base.u8[0 .. =99] is a single byte with values from 0 to 99 inclusive base.u16[0 .. =99] is a 16-bit type with the same values base.u16[100 .. =400] is still 16 bits but values from 100 to 400 inclusive In WUFFS all arithmetic overflow or array bounds misses are compile time errors. From the point of view of WUFFS if pixels is an array with 200 elements, and idx is a base.u8[0.. = 240] then pixels[idx] is a type error as it wouldn't make sense to index 240 into an array of 200 elements so that doesn't compile. As well as the obvious safety implication, this also means WUFFS can in principle go just as fast as if you were inhumanly careful with a conventional low level language and skipped any safety checks, since it knows it already took care of that. In practice it transpiles to C (which doesn't know what safety checks are).
- jeffffff 4y agoi think you could actually do that in C++ as a library with a bit of metaprogramming. the arithmetic operations will get weird though unless everything is the same type, for example what is the result type of int<-10, 10>+int<0, 20>? (the "right" answer is probably a megabyte of compiler errors)
- evouga 4y agoSurely just `int`?