DIP 1003: remove `body` as a keyword

Sönke Ludwig via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Nov 21 03:33:07 PST 2016


Am 19.11.2016 um 22:16 schrieb Dicebot:
> DIP 1003 is merged to the queue and open for public informal feedback.
>
> PR: https://github.com/dlang/DIPs/pull/48
> Initial merged document:
> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1003.md
>
> If you want the change to be approved and have ideas how to improve it
> to better match on
> https://github.com/dlang/DIPs/blob/master/GUIDELINES.md and existing
> published reviews - please submit new PR with editorial and ping
> original author.

I'd really like to see option 3, combined with option 1. The verbosity 
of the current syntax, as well as the sub-optimal contract semantics for 
classes*, make me personally almost always resort to normal assertions 
in the function body instead of using contracts.

That "body" is not available as an identifier is also quite annoying for 
certain applications. This is the case for a bunch of other keywords, 
too, but "body" as a keyword just has extremely little value, so it does 
stick out.

Really nice would be if "in" and "out" would then also take a general 
statement instead of just a block statement, so that a syntax like this 
would become possible for simple contracts:

     void foo(int a, int b)
       in assert(0 <= a && a < b);
       out(ret) assert(ret < b);
     {
       return b - 1 - a;
     }

The current equivalent just looks crowded and becomes hard to read, or 
wastes lots of vertical space if braces are put on their own line:

     void foo(int a, int b)
       in { assert(0 <= a && a < b); }
       out(ret) { assert(ret < b); }
     body {
       return b - 1 - a;
     }

For this whole proposal to work out, though, I think the old syntax will 
have to stay supported without deprecations, because the amount of 
breakage (the deprecation path won't change that) will otherwise 
probably be huge. Making "body" optional + contextual seems to be the 
way to go.

Since "body" is only used in a tiny and specific grammatical niche of 
the language, I also think that Walter's generic arguments form the 
linked thread don't really apply here, similar to "scope(...)" or 
"extern(...)".




* Example:

     interface Foo {
       void foo(int x) in { assert(x < 10); };
     }

     class Bar : Foo {
       // no contract enforced, because an omitted contract always
       // counts as a passing contract - need in { assert(false); } here
       override void foo(int x) { ... }
     }



More information about the Digitalmars-d-announce mailing list