www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Strengthening contract

reply "Kagamin" <spam here.lot> writes:
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?
Nov 11 2014
parent reply "Idan Arye" <GenericNPC gmail.com> writes:
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...
Nov 11 2014
parent reply "Kagamin" <spam here.lot> writes:
I mean, it's something to keep in mind, as I guess, such 
limitation is not documented and not obvious.
Nov 12 2014
parent "David Nadlinger" <code klickverbot.at> writes:
On Wednesday, 12 November 2014 at 10:15:45 UTC, Kagamin wrote:
 I mean, it's something to keep in mind, as I guess, such 
 limitation is not documented and not obvious.
It is documented: http://dlang.org/contracts (although it just says "no useful effect" instead of "illegal") David
Nov 12 2014