www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - property not available for classes?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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
Jan 01
next sibling parent SimonN <eiderdaus gmail.com> writes:
On Friday, 1 January 2016 at 10:14:58 UTC, Shriramana Sharma 
wrote:
     auto p = TimeSpan(1, 2);
 Error: no property 'opCall' for type '<src>.TimeSpan'
The error should be in 'auto p = ...', not in the line using the property. Instantiate with 'new TimeSpan(1, 2)' instead of 'TimeSpan(1, 2)'. The latter would be the constructor call for a struct. Classes go on the GC'ed heap by default. The property syntax should work. :-) -- Simon
Jan 01
prev sibling parent reply John <johnch_atms hotmail.com> writes:
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.
Jan 01
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
John wrote:

 It's nothing to do with the  property attribute. So you need to
 define a constructor. Also, use "new" when creating instances.
Thanks Simon and John. First actual usage of D classes and mistaken assumption that C++ syntax is valid. :-) -- Shriramana Sharma, Penguin #395953
Jan 01
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/1/16 9:08 PM, Shriramana Sharma wrote:
 John wrote:

 It's nothing to do with the  property attribute. So you need to
 define a constructor. Also, use "new" when creating instances.
Thanks Simon and John. First actual usage of D classes and mistaken assumption that C++ syntax is valid. :-)
class constructor requirements are much different from struct constructor requirements. There's also no implicit constructor that initializes all members as there is for structs. -Steve
Jan 03
parent reply Jacob Carlborg <doob me.com> writes:
On 2016-01-03 18:48, Steven Schveighoffer wrote:

 class constructor requirements are much different from struct
 constructor requirements. There's also no implicit constructor that
 initializes all members as there is for structs.
To clarify, there's a default (implicit) constructor that initializes all members to what they are set to in the class declaration. But you cannot pass in any arguments to the default constructor. Hmm, technically that might actually not be the constructor that initializes the members, not sure. -- /Jacob Carlborg
Jan 03
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/3/16 2:25 PM, Jacob Carlborg wrote:
 On 2016-01-03 18:48, Steven Schveighoffer wrote:

 class constructor requirements are much different from struct
 constructor requirements. There's also no implicit constructor that
 initializes all members as there is for structs.
To clarify, there's a default (implicit) constructor that initializes all members to what they are set to in the class declaration. But you cannot pass in any arguments to the default constructor. Hmm, technically that might actually not be the constructor that initializes the members, not sure.
Technically, the GC initializes the data as given by the TypeInfo before the ctor is run. I believe the default ctor does nothing, not even sure if it exists or is called. For simplicity, you can assume there is one. But yes, I was referring to the struct ctor that allows you to initialize one or more of the members to non-default values. -Steve
Jan 04