DIP 1003 Formal Review

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue May 16 11:57:37 PDT 2017


On Tue, May 16, 2017 at 08:43:01PM +0200, Timon Gehr via Digitalmars-d wrote:
> On 16.05.2017 20:06, Andrei Alexandrescu wrote:
> > On 05/16/2017 01:59 PM, Random D user wrote:
> > > 
> > > int foo()
> > > in
> > > {
> > > }
> > > out
> > > {
> > > }
> > > do
> > > {
> > >   bar();
> > > }
> > 
> > Can't deny I like that. -- Andrei
> 
> Beats options 2 and 3.

To me, it's actually worse, because now you have a visual conflation
with do-loops.

Overall, what I don't like about contract syntax is that it is so
unbearably verbose. It's not just the in and out blocks and the (IMO
redundant) marking of the function body; it's also the repeated
'assert's that occur in the in and out blocks.

	int foo(T, U)(T t, U u)
	if (sigContraints!T && sigConstraints!U)
	in
	{
		assert(t > 0 && u < 10);
	}
	out(val)
	{
		assert(val > 1 && val < 5);
	}
	body
	{
		// function body here
	}

I understand this DIP is only to address the `body` part of this ugly
verbosity, but imagine how much better it would be if we could write
something like this instead:

	int foo(T, U)(T t, U u)
	if (sigConstraints!T && sigConstraints!U)
	in (t > 0 && u < 10)
	out(foo > 1 && foo < 5 )
	{
		// function body here
	}

This is just tentative example syntax, of course.  We can argue over its
fine points later, but the point is that the current syntax is far too
verbose, and can easily be reduced to half the number of lines.  Merely
changing `body` to `do` does nothing to address this, and seems to me to
be just more useless churn, replacing one bit of verbosity with another
bit of verbosity. (Not to mention the IMO very ugly syntax clash with
do-loops, which will reduce code readability even more.)


T

-- 
I think the conspiracy theorists are out to get us...


More information about the Digitalmars-d mailing list