Jump to page: 1 2
Thread overview
Issue with 2.071: Regression or valid error?
Apr 06, 2016
Andre
Apr 06, 2016
Craig Dillabaugh
Apr 06, 2016
Craig Dillabaugh
Apr 06, 2016
Daniel Kozak
Apr 06, 2016
Andre
Apr 08, 2016
9il
Apr 08, 2016
Daniel Kozak
Apr 08, 2016
Rene Zwanenburg
Apr 08, 2016
Daniel Kozak
April 06, 2016
Hi,

With 2.071 following coding does not compile anymore and somehow I feel it should compile.
The issue is with line "cat.create();".
Cat is a sub type of Animal. Animal "owns" method create and I want to call the method
create within the class Animal for cat.

Is the error message "no property create for type 'b.cat'" valid or not?

Kind regards
André

module a;
import b;

class Animal
{
	private void create() {}
	
	void foo(Cat cat)
	{
		cat.create(); // >> no property create for type 'b.cat'
	}
}

void main() {}

--------------

module b;
import a;

class Cat: Animal {};

compile with
>> rdmd a b
April 06, 2016
On Wednesday, 6 April 2016 at 15:10:45 UTC, Andre wrote:
> Hi,
>
> With 2.071 following coding does not compile anymore and somehow I feel it should compile.
> The issue is with line "cat.create();".
> Cat is a sub type of Animal. Animal "owns" method create and I want to call the method
> create within the class Animal for cat.
>
> Is the error message "no property create for type 'b.cat'" valid or not?
>
> Kind regards
> André
>
> module a;
> import b;
>
> class Animal
> {
> 	private void create() {}
> 	
> 	void foo(Cat cat)
> 	{
> 		cat.create(); // >> no property create for type 'b.cat'
> 	}
> }
>
> void main() {}
>
> --------------
>
> module b;
> import a;
>
> class Cat: Animal {};
>
> compile with
>>> rdmd a b

Not so up to date on D's OOP stuff, but don't you want create() to be protected, not private.  You can typically access a private method through a base class, which is what you are doing with cat.create().
April 06, 2016
On Wednesday, 6 April 2016 at 19:01:58 UTC, Craig Dillabaugh wrote:
> On Wednesday, 6 April 2016 at 15:10:45 UTC, Andre wrote:
clip
>
> Not so up to date on D's OOP stuff, but don't you want create() to be protected, not private.  You can typically access a private method through a base class, which is what you are doing with cat.create().

You CAN'T typically access a private ...
April 06, 2016
This should not compile. Cat cant access create because it is private. Ok
it can access it but only if you move cat into same module as animal
Dne 6. 4. 2016 17:16 napsal uživatel "Andre via Digitalmars-d-learn" <
digitalmars-d-learn@puremagic.com>:

> Hi,
>
> With 2.071 following coding does not compile anymore and somehow I feel it
> should compile.
> The issue is with line "cat.create();".
> Cat is a sub type of Animal. Animal "owns" method create and I want to
> call the method
> create within the class Animal for cat.
>
> Is the error message "no property create for type 'b.cat'" valid or not?
>
> Kind regards
> André
>
> module a;
> import b;
>
> class Animal
> {
>         private void create() {}
>
>         void foo(Cat cat)
>         {
>                 cat.create(); // >> no property create for type 'b.cat'
>         }
> }
>
> void main() {}
>
> --------------
>
> module b;
> import a;
>
> class Cat: Animal {};
>
> compile with
>
>> rdmd a b
>>>
>>


April 06, 2016
On Wednesday, 6 April 2016 at 19:22:44 UTC, Daniel Kozak wrote:
> This should not compile. Cat cant access create because it is private. Ok
> it can access it but only if you move cat into same module as animal
> Dne 6. 4. 2016 17:16 napsal uživatel "Andre via Digitalmars-d-learn" <
> digitalmars-d-learn@puremagic.com>:

Thanks for the answers.

Kind regards
André
April 07, 2016
On 4/6/16 11:10 AM, Andre wrote:
> Hi,
>
> With 2.071 following coding does not compile anymore and somehow I feel
> it should compile.
> The issue is with line "cat.create();".
> Cat is a sub type of Animal. Animal "owns" method create and I want to
> call the method
> create within the class Animal for cat.
>
> Is the error message "no property create for type 'b.cat'" valid or not?
>
> Kind regards
> André
>
> module a;
> import b;
>
> class Animal
> {
>      private void create() {}
>
>      void foo(Cat cat)
>      {
>          cat.create(); // >> no property create for type 'b.cat'
>      }
> }
>
> void main() {}
>
> --------------
>
> module b;
> import a;
>
> class Cat: Animal {};

Just FYI, you don't need a semicolon there.

>
> compile with
>>> rdmd a b

Wow, totally agree with you. Compiler shouldn't make you jump through this hoop:

void foo(Cat cat)
{
   Animal a = cat;
   a.create();
}

Please file a bug report, not sure why this happened.

-Steve
April 08, 2016
On Thursday, 7 April 2016 at 15:55:16 UTC, Steven Schveighoffer wrote:
> On 4/6/16 11:10 AM, Andre wrote:
>> [...]
>
> Just FYI, you don't need a semicolon there.
>
>>>> [...]
>
> Wow, totally agree with you. Compiler shouldn't make you jump through this hoop:
>
> void foo(Cat cat)
> {
>    Animal a = cat;
>    a.create();
> }
>
> Please file a bug report, not sure why this happened.
>
> -Steve

Why this is a bug? private methods are not virtual, are they? --Ilya
April 08, 2016
On Friday, 8 April 2016 at 06:08:38 UTC, 9il wrote:
> On Thursday, 7 April 2016 at 15:55:16 UTC, Steven Schveighoffer wrote:
>> On 4/6/16 11:10 AM, Andre wrote:
>>> [...]
>>
>> Just FYI, you don't need a semicolon there.
>>
>>>>> [...]
>>
>> Wow, totally agree with you. Compiler shouldn't make you jump through this hoop:
>>
>> void foo(Cat cat)
>> {
>>    Animal a = cat;
>>    a.create();
>> }
>>
>> Please file a bug report, not sure why this happened.
>>
>> -Steve
>
> Why this is a bug? private methods are not virtual, are they? --Ilya

No they are not virtual, so this is not a bug.

April 08, 2016
On Friday, 8 April 2016 at 08:26:11 UTC, Daniel Kozak wrote:
> On Friday, 8 April 2016 at 06:08:38 UTC, 9il wrote:
>> On Thursday, 7 April 2016 at 15:55:16 UTC, Steven Schveighoffer wrote:
>>> Wow, totally agree with you. Compiler shouldn't make you jump through this hoop:
>>>
>>> void foo(Cat cat)
>>> {
>>>    Animal a = cat;
>>>    a.create();
>>> }
>>>
>>> Please file a bug report, not sure why this happened.
>>>
>>> -Steve
>>
>> Why this is a bug? private methods are not virtual, are they? --Ilya
>
> No they are not virtual, so this is not a bug.

I disagree, virtuality has nothing to do with this. After all, moving Cat to the same module as Animal, or making create() public final will make the code compile.

Private symbols can (only) be accessed from the module where they are defined, so anything in the animal module should be able to call create() on a Cat just fine.
April 08, 2016
On 4/8/16 2:08 AM, 9il wrote:
> On Thursday, 7 April 2016 at 15:55:16 UTC, Steven Schveighoffer wrote:
>> On 4/6/16 11:10 AM, Andre wrote:
>>> [...]
>>
>> Just FYI, you don't need a semicolon there.
>>
>>>>> [...]
>>
>> Wow, totally agree with you. Compiler shouldn't make you jump through
>> this hoop:
>>
>> void foo(Cat cat)
>> {
>>    Animal a = cat;
>>    a.create();
>> }
>>
>> Please file a bug report, not sure why this happened.
>>
>
> Why this is a bug? private methods are not virtual, are they? --Ilya

A Cat is an Animal. The compiler knows this. The 'a' module has access to Animal private methods.

This isn't a virtual call, but a call to a base class member. Those are allowed. How can you think this is not a bug?

Would you think it was a bug if create was a final function instead of private? This is exactly the same thing.

-Steve
« First   ‹ Prev
1 2