Suggested Change to Contract Syntax

jmh530 via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 10 15:31:14 PST 2016


On Thursday, 10 March 2016 at 22:57:41 UTC, Jonathan M Davis 
wrote:
> IMHO, at this point, inheritance is the only reason they're 
> worth
> having in the language. [snip]
>

I created a simple example to understand your point about 
contracts only really mattering for inheritance, but my example 
is giving assertion errors for the inherited class the same way 
as the base class. What would I need to do for this issue to 
become apparent?

class A
{
	int a;
	
	this(int x)
	{
		a = x;
	}
	
	int foo(int x)
	{
		assert(x != 0);
		scope(exit) assert((this.a - x) != 0);

		return this.a - x;
	}
}

class B : A
{
	this()
	{
		super(4);
	}

}

void main()
{
	import std.stdio : writeln;
	
	auto a = new A(2);
	//writeln(a.foo(0)); //causes assertion failure
	//writeln(a.foo(2)); //causes assertion failure
	
	auto b = new B();
	//writeln(b.foo(0)); //causes assertion failure
	//writeln(b.foo(4)); //causes assertion failure
}



More information about the Digitalmars-d mailing list