| ▲ | jaggederest 3 hours ago | |||||||||||||||||||||||||||||||
You can go even further with this in other languages, with things like dependent typing - which can assert (among other interesting properties) that, for example, something like
cannot ever have index outside the bounds of the array, but checked statically at compilation time - and this is the key, without knowing a priori what the length of array is."In Idris, a length-indexed vector is Vect n a (length n is in the type), and a valid index into length n is Fin n ('a natural number strictly less than n')." Similar tricks work with division that might result in inf/-inf, to prevent them from typechecking, and more subtle implications in e.g. higher order types and functions | ||||||||||||||||||||||||||||||||
| ▲ | satvikpendem an hour ago | parent | next [-] | |||||||||||||||||||||||||||||||
Rust has some libraries that can do dependent typing too, based on macros. For example: https://youtube.com/watch?v=JtYyhXs4t6w Which refers to https://docs.rs/anodized/latest/anodized/ | ||||||||||||||||||||||||||||||||
| ▲ | VorpalWay 3 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
How does that work? If the length of the array is read from stdin for example, it would be impossible to know it at compile time. Presumably this is limited somehow? | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | esafak 2 hours ago | parent | prev [-] | |||||||||||||||||||||||||||||||
I wish dependent types were more common :( | ||||||||||||||||||||||||||||||||