[Issue 12724] Error: return statements cannot be in contracts

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun May 11 14:23:56 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=12724

--- Comment #8 from monarchdodra at gmail.com ---
(In reply to bearophile_hugs from comment #7)
> (In reply to monarchdodra from comment #6)
> 
> > What's your arguments?
> 
> What does it means returning from a test that
> verifies if a contract is holding?

It means you've tested what needs to be tested, and the input given need to
proceed with the rest of the code.

Actual example in phobos:

auto assumeSorted(Range)(Range r)
in
{
    if (range.length <= 1) return;
    //more than 1 element, do actual tests
    ...
}
body

> I think allowing returns goes against the meaning of contracts. They are not
> meant to change the flow of the code, all they have to do is to verify the
> input or outputs or invariants are correct.

"Flow of code" does not mean the code actual "does" anything, and I see nothing
wrong with having control structures in a contract.

Not being able to return in the middle of the contract is basically *forcing*
the "single point of exit" paradigm. And we know how that ends...

In any case, it's completely inconsistent with the rest of the language.
Besides, it can be just as easily worked around with:

in
{
    void doTest(){...}
    doTest();
}

or

in
{{
    if (...)
        goto end;
}end:}

Long story short: If you *do* need to prematurely end the function, you are
forced to write crap.

--


More information about the Digitalmars-d-bugs mailing list