On Friday, 1 January 2016 at 10:14:58 UTC, Shriramana Sharma
wrote:
Hello. I'm trying the following code:
import std.stdio;
class TimeSpan
{
immutable double start, end;
@property double length() { return end - start; }
}
void main()
{
auto p = TimeSpan(1, 2);
writeln(p.length);
}
...and I'm getting the error:
Error: no property 'opCall' for type '<src>.TimeSpan'
If I change the class to struct the @property is callable
without parens but I need TimeSpan to be a class since I need
to inherit from it.
http://dlang.org/property.html and
http://dlang.org/spec/function.html#property-functions don't
say anything about @property not being applicable for classes.
Am I stuck with having to use the () for this even in D?
The error is actually referring to the lack of a suitable
constructor. It thinks TimeSpan should define opCall because of
the way you're trying to create an instance your main function.
It's nothing to do with the @property attribute. So you need to
define a constructor. Also, use "new" when creating instances.
Alternatively, make it a struct and it will work as is without
further changes.