@property not available for classes?

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 1 02:14:58 PST 2016


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?

-- 
Shriramana Sharma, Penguin #395953


More information about the Digitalmars-d-learn mailing list