digitalmars.D.learn - property not available for classes?
- Shriramana Sharma (22/22) Jan 01 2016 Hello. I'm trying the following code:
- SimonN (9/11) Jan 01 2016 The error should be in 'auto p = ...', not in the line using the
- John (9/30) Jan 01 2016 The error is actually referring to the lack of a suitable
- Shriramana Sharma (5/7) Jan 01 2016 Thanks Simon and John. First actual usage of D classes and mistaken
- Steven Schveighoffer (5/10) Jan 03 2016 class constructor requirements are much different from struct
- Jacob Carlborg (8/11) Jan 03 2016 To clarify, there's a default (implicit) constructor that initializes
- Steven Schveighoffer (7/16) Jan 04 2016 Technically, the GC initializes the data as given by the TypeInfo before...
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? --
Jan 01 2016
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 2016
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 2016
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. :-) --
Jan 01 2016
On 1/1/16 9:08 PM, Shriramana Sharma wrote:John 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. -SteveIt'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. :-)
Jan 03 2016
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 2016
On 1/3/16 2:25 PM, Jacob Carlborg wrote:On 2016-01-03 18:48, Steven Schveighoffer wrote: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. -Steveclass 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.
Jan 04 2016