Rust code to assert a runtime precondition or postcondition a > b then handle it as you wish:
assert_gt_as_result!(a, b) -> Result
https://crates.io/crates/assertablesRust code to assert a runtime precondition or postcondition a > b then handle it as you wish:
assert_gt_as_result!(a, b) -> Result
https://crates.io/crates/assertablesWhich you have to prove, which is absolutely non-trivial. I guess it could be made into a runtime check as well where static analysis is not possible, but that seems to be a quite hard to reason about language regarding performance.
He's not, he would respect your opinion, but design by contract was on of the key foundations of Eiffel, and I have a soft spot for Eiffel.
Also: Maybe not everything goes in the compiler?
https://old.reddit.com/r/rust/comments/17miqiu/is_ada_safer_...
In the design by contract case, there's a lot of experimentation (I linked this https://old.reddit.com/r/rust/comments/17miqiu/is_ada_safer_... in another comment) and while all of them are converging to the same syntax, they all have wildly different implementations.
Perhaps the stdlib could provide a base syntax for contracts, with extensibility APIs so that libraries or compiler plugins or external tooling can determine whether the contract is checked at runtime or at compile time, and by which method. But that demands a lot of design, because once something is in the stdlib and stabilized, it's here forever.
https://learn.microsoft.com/en-us/dotnet/framework/debug-tra...
The most typical example, in C, output arguments, passed by pointer, a pattern that I also use in C++ because I find passing by non-const reference error-prone since it is not obvious looking at the calling code that an argument may be modified.
But what about NULL? Sometimes it is a good thing that NULL is allowed, sometimes, it doesn't make sense. So again, not obvious. So what to we do? You can document it of course, but the problem is that compilers don't read the docs, and therefore can't tell you if you are doing it wrong. You can play it safe, and design your API so that NULL is always an option, and avoid NULL when using others APIs, but it leads to unnecessary and performance-impacting checks. Preconditions and postconditions would solve that problem: if you pass an argument that can be NULL to a function that doesn't accept NULL, the compiler can warn you. Plus, there is optimization potential, in the same way that C/C++ does with undefined behavior, but this is explicit. You also can also get the choice between safe (runtime checks) or fast (undefined behavior) at compile time.
Making it a core feature of the language rather than an extension (like your rust crate) will help compilers and other tools to take full advantage of what it brings.
I don't think there's all that much that a typical compiler will do with them however sophisticated enough type system I can see some wins.
https://learn.microsoft.com/en-us/cpp/code-quality/using-sal...
https://www.google.com/url?sa=t&source=web&rct=j&opi=8997844...
We have a linter that will try to catch cases where you didn't null check but the field is nullable
VS code makes it pretty obvious by adding a visual & prefix to the argument. I hope that kind of visual help becomes widely adopted.
assert_gt!(value1, value2); // value1 ≥ value2
That should be ">" rather than "≥". (Or "assert_ge" rather than "assert_gt".)For what you're asking, the assertables crate has a macro that will handle any condition:
assert_as_result!(condition) -> Result
And you can alias it as you wish such as: use assertables::assert_as_result as precondition;
And now you can write what you're suggesting: precondition(a == b)