5 ms·
This seems vaugely similar to C# Code Contracts. The difference there is that it was embedded as an assertion into the method: Contract.Requires(start > 0)
by zamalek 3y ago
This seems vaugely similar to C# Code Contracts. The difference there is that it was embedded as an assertion into the method:
Contract.Requires(start > 0);
Contract.Ensures(someReturn != null);
I loved it. It gave you much (not all) of that "if it compiles it works" good vibes that Rust has. Though, oh boy, were you in for a surprise if you think that Rust compiles slowly. You could probably do something similar with C:
CONTRACT_REQUIRE(x == 0 || x == -1);
if(x) {
void* result = malloc(1);
CONTRACT_ENSURE(result != null);
CONTRACT_CALLER(free(result));
return result;
} else {
CONTRACT_ENSURE(result == 0);
return 0;
}
The nice thing about that is you could have a compatibility .h that asserts/nops the macros, making the code compile in ignorant compilers.
- thesuperbigfrog 3y agoYou can use "Design by Contract" (https://en.wikipedia.org/wiki/Design_by_contract https://en.wikipedia.org/wiki/Design_by_contract) in Rust: https://docs.rs/contracts/latest/contracts/ https://docs.rs/contracts/latest/contracts/ Ada has Design by Contract built into the language (not a library): https://learn.adacore.com/courses/intro-to-ada/chapters/contracts.html https://learn.adacore.com/courses/intro-to-ada/chapters/cont... DbC is awesome because it formalizes what conditions must exist before and after a piece of code runs and defines who (caller vs. callee) is responsible for what parts.