Suggested Change to Contract Syntax

jmh530 via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 10 20:17:51 PST 2016


On Friday, 11 March 2016 at 01:45:36 UTC, Jonathan M Davis wrote:
>
> Sure, but if you're not using in/out contracts with a class, 
> you're not going to see how they interact with inheritance. To 
> mimic what they do, you'd have to duplicate the base class 
> contracts in the derived class and make sure that you ||ed the 
> in contracts correctly and &&ed the out contracts correctly, 
> which isn't very maintainable.
>
> - Jonathan M Davis

If I'm understanding you correctly, you could still get the same 
behavior with something like below (though it isn't exactly right 
since Base.foo's in block would technically have a funky rule 
applied to it).

After playing around with your example, I'm finding in/out blocks 
on derived classes to be tricky. I think I'm going to try to 
avoid putting myself in a situation where I would screw something 
up.

import core.exception;
import std.exception;

class Base
{
	void baseCheck(int i)
	{
		assert(i % 2 == 0, "base out failed");
	}

     int foo(int i)
     {
		assert(i > 0 && i < 10, "base in failed");
		scope(exit) baseCheck(i);

         return i;
     }
}

class Derived : Base
{
     override int foo(int i)
	{
		scope(exit) baseCheck(i);
		
         assert(i < 50, "derived in failed");
		scope(exit) assert(i % 3 == 0, "derived out failed");

         return i;
     }
}


More information about the Digitalmars-d mailing list