D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10396 - Optional, simplified syntax for simple contracts
Summary: Optional, simplified syntax for simple contracts
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-17 18:20 UTC by Tommi
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tommi 2013-06-17 18:20:47 UTC
"Simple things should be simple, complex things should be 
possible." -Alan Kay

I'd like to simplify the syntax of function pre- and 
post-conditions when the contract block consists of a single 
assert statement. A special syntax for this special case would 
omit all of the following:
1) the block's curly braces
2) the assert keyword
3) the semi-colon ending the assert statement
4) the body keyword (if and only if it's right after the block)

So, instead of writing this:

int func(int i)
in
{
    assert(i < 5);
}
out(r)
{
    assert(r < 9);
}
body
{
    return i * 2;
}

...you'd be able to write this:

int func(int i)
in (i < 5)
out(r) (r < 9)
{
    return i * 2;
}
Comment 1 Marco Leise 2013-06-19 13:00:50 UTC
There I fixed it: http://dpaste.1azy.net/2ec082c0 :)

--- 8< ------------------

string holds(string expr, string expl = null)
{
	import std.array : split;
	import std.string : strip;

	expr = strip(expr);
	string vars;
	bool[string] processed;
	foreach (token; split(expr)) if (token.length && token !in processed)
	{
		if (token[0] == '.' || token[0] >= 'A') {
			if (vars !is null) vars ~= ", ";
			vars ~= token ~ ` = " ~ to!string(` ~ token ~ `) ~ "`;
			processed[token] = true;
		}
	}
	if (expl) expl = " " ~ expl ~ ";";
	return `import std.conv : to; import std.array : join; import std.typetuple : ParameterIdentifierTuple; `
		~ `assert(` ~ expr ~ `, __FUNCTION__ ~ "(" ~ join([ParameterIdentifierTuple!(mixin(__FUNCTION__))], ", ") ~ "):` ~ expl ~ ` (` ~ expr ~ `)` ~ ` failed with ` ~ vars ~ `");`;
}

void foo(uint x, uint y)
in { mixin(q{ x > y && x < 10 }.holds("x must be a digit larger than y")); }
body {}

void main()
{
	foo(2, 3);
}

--- >8 ------------------

Prints:

foo(x, y): x must be a digit larger than y; (x > y && x < 10) failed with x = 2, y = 3
Comment 2 basile-z 2020-02-20 10:57:55 UTC
done, this was DIP 1009, https://raw.githubusercontent.com/dlang/DIPs/master/DIPs/accepted/DIP1009.md