Ask HN: Is it possible to perform compile-time checks for all invariants?
Static typing helps with compile-time checks for types. I'd like to know from the community here if it's possible to check for other types of invariants at compile-time?
Let's say, I've got a function increase_balance(increment_value: int) but instead of allowing all possible integer values, I want the compiler to check that anytime this function is called, it is called with increment_value in the range 1 to 10000.
Is it theoretically possible to do such checks at compile-time? I mean the compiler has to check every code path and verify that all code paths leading to increase_balance() leads to value that is neither <= 0 nor >= 10001. Is this is a problem that can be solved at compile-time? Does any programming language provide such features?
While I've your attention, are there other such invariants you're aware of that can be checked during compile-time?
4 comments
[ 5.1 ms ] story [ 23.3 ms ] threadSome languages like F* are also proof oriented.
Overall, there's no free lunch: somewhere, someone will have to do some extra work.
Other useful invariants include some RAII idioms, Rust memory checks, resource usage (e.g. locks), etc.