Package functions cannot be abstract?!

Neia Neutuladh neia at ikeran.org
Wed Jan 30 04:23:22 UTC 2019


On Wed, 30 Jan 2019 04:05:15 +0000, Jonathan Levi wrote:
> Why in the world can abstract functions not be only visible to only a
> package?

`package`-protected functions are not overridable. Only functions that are 
visible to all derived classes can be overridden.

This is in the spec: https://dlang.org/spec/function.html#virtual-functions
"Member functions which are private or package are never virtual, and 
hence cannot be overridden."

This seems pretty weird on reflection. Visibility and virtual dispatch 
seem orthogonal.

To make your thing work, I'd write a package function that forwards to a 
protected function:

class Cls
{
  package void fun(string a) { return funImpl(a); }
  protected abstract void funImpl(string a);
}

This isn't exactly the same; a subtype in a different package could 
override this function, which might be undesirable. As a workaround:

package struct Hidden {}
class Cls
{
  package void fun(string a) { return funImpl(a, Hidden.init); }
  protected abstract void funImpl(string a, Hidden h);
}

For a subclass in a different package to override funImpl, it would have 
to access Hidden, which it shouldn't be able to do.


More information about the Digitalmars-d mailing list