DIP 1009--Improve Contract Usability--Preliminary Review Round 1
MysticZach via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jun 23 05:19:51 PDT 2017
On Friday, 23 June 2017 at 09:06:59 UTC, Solomon E wrote:
> What I expected from my impression of existing D syntax was
> that something like this might be coming up:
>
> T foo(T)(T x, T y)
> in (x > 0, y > 0)
> out (r; r > 0)
> {
> return x % y + 1;
> }
>
> `out ()` has syntax similar to `foreach` and `for`, allowing
> mentioning something about the return type, or unpacking a
> return value, then testing it. (When there's no semicolon, an
> `out` block follows.)
This is a good idea, and I didn't think of it. Points:
* It would be great if you were allowed to omit the identifier
before the semicolon in cases where you didn't need it. The
existing foreach grammar does not allow this [1]. I'm not sure if
that is intended to prevent bugs, or if it was just never thought
to be useful.
* The grammar for what's inside the parens should also mimic
what's inside `assert`s, allowing an error message. So the new
grammar would actually be a mix of the existing `foreach` and
`assert` grammars, e.g.
out(r; r >0, "contract violated!")
Comparing with the double parens solution:
out(result)(result > 0, "violation!")
out()(otherData > 0, "prohibited!")
vs.
out(result; result > 0, "violation!")
out(; otherData > 0, "prohibited!")
It's a close call. Nice job!
[1] https://dlang.org/spec/statement.html#foreach-statement
More information about the Digitalmars-d
mailing list