www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overriding a property ?

reply Lucien <lucien.perregaux gmail.com> writes:
How can I override a property ?

Test code:
----------------------------
class A
{
      property bool foo { return false; }
     void myFunc() { ... }
}

class B : A
{
     override bool foo { return true; } // error
     override void myFunc() { ... }
}
----------------------------
Output error:
function B.foo return type inference is not supported if may 
override base class function.

Any ideas ?
Apr 14 2016
next sibling parent Satoshi <satoshi gshost.eu> writes:
On Thursday, 14 April 2016 at 20:21:38 UTC, Lucien wrote:
 How can I override a property ?

 Test code:
 ----------------------------
 class A
 {
      property bool foo { return false; }
     void myFunc() { ... }
 }

 class B : A
 {
     override bool foo { return true; } // error
     override void myFunc() { ... }
 }
 ----------------------------
 Output error:
 function B.foo return type inference is not supported if may 
 override base class function.

 Any ideas ?
try class A { property bool foo() { return false; } void myFunc() { ... } } class B : A { override property bool foo() { return true; } // error override void myFunc() { ... } }
Apr 14 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/14/16 4:21 PM, Lucien wrote:
 How can I override a property ?

 Test code:
 ----------------------------
 class A
 {
       property bool foo { return false; }
This isn't valid, you need parentheses for foo. This doesn't compile does it?
      void myFunc() { ... }
 }

 class B : A
 {
      override bool foo { return true; } // error
Same thing here.
      override void myFunc() { ... }
 }
 ----------------------------
 Output error:
 function B.foo return type inference is not supported if may override
 base class function.
This has to do with type inference. Did you not specify bool in your original? If I put parens on both foo, then it compiles for me. -Steve
Apr 14 2016
parent reply Lucien <lucien.perregaux gmail.com> writes:
On Thursday, 14 April 2016 at 20:39:50 UTC, Steven Schveighoffer 
wrote:
 On 4/14/16 4:21 PM, Lucien wrote:
 How can I override a property ?

 Test code:
 ----------------------------
 class A
 {
       property bool foo { return false; }
This isn't valid, you need parentheses for foo. This doesn't compile does it?
      void myFunc() { ... }
 }

 class B : A
 {
      override bool foo { return true; } // error
Same thing here.
      override void myFunc() { ... }
 }
 ----------------------------
 Output error:
 function B.foo return type inference is not supported if may 
 override
 base class function.
This has to do with type inference. Did you not specify bool in your original? If I put parens on both foo, then it compiles for me. -Steve
You're right. In fact it didn't work because I did: ---------------------------- class A { property foo() { return false; } void myFunc() { } } class B : A { override property foo() { return true; } // error override void myFunc() { } } ---------------------------- When I remove the bool, the program compile but show the error when overriding. Is it a bug ?
Apr 14 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/14/16 4:54 PM, Lucien wrote:
 You're right.
 In fact it didn't work because I did:

 ----------------------------
 class A
 {
       property foo() { return false; }
      void myFunc() {  }
 }

 class B : A
 {
      override  property foo() { return true; } // error
      override void myFunc() {  }
 }
 ----------------------------

 When I remove the bool, the program compile but show the error when
 overriding.
 Is it a bug ?
Hm... I don't know. the error specifically says it's not supported. But it seems to me odd that it wouldn't. What is the harm in letting the derived function use inferred return type? I don't know the reasoning behind the error condition, so I can say it's definitely not a bug, but I don't know why it's this way. Perhaps there's some situation that I'm not thinking of. -Steve
Apr 14 2016