Package permission and symbol resolution

Manu via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 22 01:07:21 PDT 2014


So I've restructured one of my projects which is a C library bindings,
but also with some D-ification.

I separated it into 2 parts, the raw binding parts, and the api
enhancements, the module structure looks like this:

The Raw C binding part:

pkg/c/module.d:

  pkg.c.module;

  struct ModuleStruct
  {
    package:
      int privateParts;
  }

  extern (C) void func(const(char)* pString);


And the wrapping layer; public import's the raw C bindings, which is
meant to make the C API available too while adding some sugar for
convenience.

pkg/module.d:

  pkg.module;

  public import pkg.c.module;

  void func(const(char)[] str)
  {
    pkg.c.module.func(str.toStringz);
  }

  void func2(ref ModuleStruct s)
  {
    s.privateParts += 10;
  }


I have a bunch of surprising problems.

1. Error: struct pkg.c.module.ModuleStruct member privateParts is not accessible

pkg.module can't access pkg.c.module.ModuleStruct.privateParts. Isn't
that what 'package' protection is for? My sugar wrapping module needs
to be able to access the private bits of the raw binding module, which
I don't want to pollute with sugar directly (since it would ideally be
generated by a generator and reflect only a pure C binding).

2. In client code, when I call for instance, func("string".ptr), which
has 2 overloads: 'extern (C) void func(const(char)*)' and 'void
func(const(char)[])', it doesn't seem to resolve the function
correctly:

Error: pkg.module.func at pkg\module.d(14) conflicts with
pkg.c.module.func at pkg\c\module.d
Error: function pkg.module.func (const(char)[] str) is not callable
using argument types (immutable(char)*)

Obviously, the other overload (pkg.c.module.func) can receive
immutable(char)* though, but it just ignores it rather than choosing
to call the appropriate overload.
What's really weird, is if I pass a string instead: func("string"[]),
it picks the other incorrect overload.

Error: cannot implicitly convert expression (func("string"[])) of type
const(char)[] to const(char)*


I've changed lots of random things, but I can't seem to resolve this cleanly.
What have I missed? It must be something simple.
This must be a common problem. I would imagine basically anyone who
wants to add sugar to a C binding would do it this way to avoid
polluting of the raw binding code?
Is there an alternative recommended practise?


More information about the Digitalmars-d mailing list