My design need friends

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Oct 5 17:36:17 PDT 2013


On 10/6/13, Namespace <rswhite4 at googlemail.com> wrote:
> Any further suggestions or improvements?

That's nice.

You could also hack away with templates, extracting the full module
path (e.g. foo.bar) of the calling module and comparing this with the
module a symbol is in, and then do this sort of thing:

In a module like foolib.mod:

- If some API-marked function is called from any submodule of "foolib"
=> compilation proceeds
- Otherwise, statically assert.

However I've just realized we are missing a __MODULE__ equivalent
which gives us the full path of the module. For example, for a.b.c
returns c, rather than a.b.c.

Adam D. Ruppe will probably understand where I'm going with this. Just
to pseudocode a bit:

test.d:

-----
module test;

import foo.a;
import foo.b;

void main()
{
    B b;
    b.func();  // this will fail
}
-----

foo/a.d:
-----
module foo.a;

import foo.b;

void test()
{
    B b;
    b.func();  // ok, compiles
}
-----

foo/b.d:
-----
module foo.b;

import foo.internal;

struct B
{
    // introduce public function "func" only accessible from any 'foo'
submodules
    // which forwards to "funcImpl"
    mixin Internal!(funcImpl, "func");

    private void funcImpl() { }  // internal
}
-----

foo/internal.d:
-----
module foo.internal;

mixin template Internal(alias symbol, string funcIdent)
{
    mixin("
    public auto " ~ funcIdent ~ "(string packName = __PACKAGE__, T...)(T t)
    {
        // note: not the actual assert, you would probably do string
comparisons here,
        // e.g. checking whether both packages start with "foo."
        static assert(__PACKAGE__ == packName);
        return symbol(t);
    }
    ");
}
-----

The only issue is there's no way to get to the caller's fully
qualified module name. You could hack around extracting this info from
a __PRETTY_FUNCTION__ string, but it's unreliable.

I think we may need a new __PACKAGE__ symbol.


More information about the Digitalmars-d-learn mailing list