Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 14, 2016 Overriding a property ? | ||||
---|---|---|---|---|
| ||||
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 ? |
April 14, 2016 Re: Overriding a property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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() { ... }
}
|
April 14, 2016 Re: Overriding a property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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 |
April 14, 2016 Re: Overriding a property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 ?
|
April 14, 2016 Re: Overriding a property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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
|
Copyright © 1999-2021 by the D Language Foundation