Functions with package protection

grauzone none at example.net
Wed Jun 3 16:06:24 PDT 2009


Robert Fraser wrote:
> grauzone wrote:
>>> Sorry to dig up this old post, but I still don't understand why 
>>> 'package' functions cannot be virtual? Is there a good reason for 
>>> this? I can't see why we can't use polymorphism on 'package' functions!
>>>
>>> Is there way to make it virtual without making it public? (e.g. a 
>>> 'virtual' keyword?)
>>
>> "package" needs to fixes:
>> - package methods must be allowed to be virtual
>> - package methods must be allowed to be accessed from sub packages 
>> (module a.b.x should be able to access package identifiers declared in 
>> module a.y)
>>
>> I don't understand why these fixes applied, especially because they 
>> are completely backward compatible.
> 
> "package methods must be allowed to be virtual" isn't 
> backwards-compatible. This code will work differently if package methods 
> were made virtual:
> 
> class A
> {
>     package void foo() { printf("A"); }
> }
> 
> class B
> {
>     package void foo() { printf("B"); }
> }
> 
> void main()
> {
>     A a = new B();
>     a.foo();
> }

I'd say this code relied on a bug, and fixing bugs is always allowed. 
Actually, it is very likely that the behavior above wasn't even intended 
by the programmer.



More information about the Digitalmars-d mailing list