Strengthening contract

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 11 09:53:17 PST 2014


On Tuesday, 11 November 2014 at 15:31:07 UTC, Kagamin wrote:
> http://forum.dlang.org/post/jftnpqyahnwacgkmslej@forum.dlang.org
>
> c:\D\import\dfl\button.d(381): Error: function
> dfl.button.Button.text cannot have
> e an in contract when overriden function 
> dfl.control.Control.text
> does not have an in contract
>
> This is the line 381:
>
> override void text(Dstring txt) // setter
> in
> {
> 	if(txt.length)
> 		assert(!this.image, "Button image with text not supported");
> }
> body
> {
> 	super.text = txt;
> }
>
> But if a button can't display image with text by design, it's 
> its contract, that text can't be set with image, while the base 
> class has no such restriction. What does it mean, DbC is 
> incompatible with inheritance?

Contracts are a tool for enforcing the Liskov substitution 
principle(http://en.wikipedia.org/wiki/Liskov_substitution_principle). 
What you are trying to do violates the LSP because I can't set 
the text of a Control if what I have in hand happens to be it's 
subclass, Button.

Now, I'm not saying that you shouldn't do that assert - I think 
the best-practices work better as rules of thumb than as sacred 
religious rules - but since contracts are modeled after the LSP 
they are probably not the right tool for this job...


More information about the Digitalmars-d mailing list