Preconditions Should be Checked Explicitly
twistedoakstudios.com
twistedoakstudios.com
Currently we have 3 (ugly) options:
1: Validate before method call (bad for obvious reasons)
2: Validate immediately within method (does the job, but looks ugly).
3: Add some kind of pre-processing via method annotations which will do the validation for you.
Wouldn't it be better if we could assert the validation requirements directly within the method definition?
For example:
public boolean doSomethingFor(10 > int iterations > 0)...
Obviously this isn't the most concise example, but I think it demonstrates the point. I guess the problem with this is it makes future changes to the preconditions confusing / difficult - depending upon how exactly this feature would end up being implemented. I'm imagining that this wouldn't interfere directly with the method signature, and would instead be turned into code much like the one in the example during compilation. public boolean doSomethingFor(int iterations) {
// this is considered part of the semantic signature
requires iterations > 0 && iterations < 10;
// implementation
...
}
Putting it in the types is a good idea, but extremely complicated. You need full dependent types to make it work.If they did turn out to be a bottleneck in the future then modifying or even removing them shouldn't affect visible behavior.