www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why do 'abstract' methods with 'in' or 'out' contracts require a body?

reply "Trey Brisbane" <tbrisbane hotmail.com> writes:
Hey all,

I have a class method defined like so:

abstract class MyClass {
public:
	 property
	abstract SomeClassType getField() pure nothrow
	out(result) {
		assert(result !is null, "Error: getField() returned null.");
	}
}

As you can see, this method is abstract, as well as in an 
abstract class. Why, then, do I get the following error when 
compiling it?

"Error: function module.MyClass.getField in and out contracts 
require function body"

Is this a compiler bug, or is there a reason for this?

Thanks for your time!
Sep 12 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 13 September 2014 at 05:07:32 UTC, Trey Brisbane 
wrote:
 Hey all,

 I have a class method defined like so:

 abstract class MyClass {
 public:
 	 property
 	abstract SomeClassType getField() pure nothrow
 	out(result) {
 		assert(result !is null, "Error: getField() returned null.");
 	}
 }

 As you can see, this method is abstract, as well as in an 
 abstract class. Why, then, do I get the following error when 
 compiling it?

 "Error: function module.MyClass.getField in and out contracts 
 require function body"

 Is this a compiler bug, or is there a reason for this?

 Thanks for your time!
I thought it was an error, but then I found this in the documentation: http://dlang.org/attribute.html#abstract "Functions declared as abstract can still have function bodies. This is so that even though they must be overridden, they can still provide ‘base class functionality.’" => it's intentional
Sep 13 2014
next sibling parent "Trey Brisbane" <tbrisbane hotmail.com> writes:
On Saturday, 13 September 2014 at 07:32:23 UTC, Marc Schütz wrote:
 I thought it was an error, but then I found this in the 
 documentation:

 http://dlang.org/attribute.html#abstract

 "Functions declared as abstract can still have function bodies. 
 This is so that even though they must be overridden, they can 
 still provide ‘base class functionality.’"

 => it's intentional
Can != must ;) Interesting fact, but it still looks like a bug to me. The contract shouldn't be forcing a body on an abstract method regardless of whether or not abstract methods are allowed to have bodies.
Sep 13 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Marc Schütz:

 "Functions declared as abstract can still have function bodies. 
 This is so that even though they must be overridden, they can 
 still provide ‘base class functionality.’"

 => it's intentional
But also they can not have. I don't think it's intentional. I think it's a temporary limitation. Search in Bugzilla. Bye, bearophile
Sep 13 2014
prev sibling parent "KlausAlmaust" <KlausAlmaust gmx.de> writes:
On Saturday, 13 September 2014 at 07:32:23 UTC, Marc Schütz wrote:
 On Saturday, 13 September 2014 at 05:07:32 UTC, Trey Brisbane 
 wrote:
 Hey all,

 I have a class method defined like so:

 abstract class MyClass {
 public:
 	 property
 	abstract SomeClassType getField() pure nothrow
 	out(result) {
 		assert(result !is null, "Error: getField() returned null.");
 	}
 }

 As you can see, this method is abstract, as well as in an 
 abstract class. Why, then, do I get the following error when 
 compiling it?

 "Error: function module.MyClass.getField in and out contracts 
 require function body"

 Is this a compiler bug, or is there a reason for this?

 Thanks for your time!
I thought it was an error, but then I found this in the documentation: http://dlang.org/attribute.html#abstract "Functions declared as abstract can still have function bodies. This is so that even though they must be overridden, they can still provide ‘base class functionality.’" => it's intentional
interfaces as well, if the final attribute is specified, which is a nice feature imo, since you reason in the interface scope.
Sep 13 2014