Issue 4720 - contracts don't work on function definitions
Summary: contracts don't work on function definitions
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: contracts
Depends on:
Blocks:
 
Reported: 2010-08-24 05:23 UTC by Trass3r
Modified: 2017-07-01 18:46 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Trass3r 2010-08-24 05:23:06 UTC
The Matlab C headers provide some kind of hand-crafted contracts by defining extra functions with suffix "_d" that check all the arguments and then detouring the calls to the original functions via the preprocessor (#define foo foo_d)

I tried to leverage D's built-in contract programming but it doesn't work:

module externcontracts;
import externcontracts2;
void foo(int a) in {assert(a>=0);}

module externcontracts2;
void foo(int a){}

yields:
externcontracts.d(3): Error: function externcontracts.foo in and out contracts require function body
resp.
externcontracts.d(3): missing body { ... } after in or out
if you put a ';' after the in{} block


Note that this is exactly the same syntax as for interface contracts.
Comment 1 Andrei Alexandrescu 2013-02-08 05:07:16 UTC
What is the expected use and effect of the code show? (Not sure I'm getting it.) With or without contracts, externcontracts.foo and externcontracts2.foo are distinct functions, so automatic forwarding from one another won't happen.

One obvious workaround would be:

module externcontracts;
import externcontracts2;
void foo(int a) in {assert(a>=0);} { return externcontracts2.foo(a); }

which may be automated in a number of ways (introspection, mixin etc).
Comment 2 Vladimir Panteleev 2017-07-01 18:46:00 UTC
No clarification in 4 years, closing :)