Can't pass private symbols as alias parameter

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 17 06:13:21 PST 2014


On 11/16/14 5:41 AM, deadalnix wrote:
> module a;
>
> struct A(alias foo) {
>      auto foo() {
>          return foo();
>      }
> }

I note here, the above doesn't is incorrect regardless, I assume you 
didn't actually try to compile this post example :)

To fix, I did this:

struct A(alias foo) {
   auto bar() {
      return foo();
   }
}

>
> module b;
>
> import a;
>
> void main() {
>      auto a = A!bar();
> }
>
> private int bar() { return 42; }
>
> This do not work. I think it is a bug but I see how could see it as a
> feature. Which one is it ?

I think it is intentional, even if it's not desirable.

Note that A is treated as if it is in a's namespace, so it cannot access 
functions that a does not define or that it cannot access publicly 
through its imports.

The alias cannot override the protection attributes, and I'm pretty sure 
that is intentional.

I tried some workarounds:

void main() {
   public alias b = bar;
   auto a = A!b();
}

It would be nice if that worked, because it does not expose bar except 
in this one case, and it identifies that you know bar has become public 
for this one case. But unfortunately, it has the same issue.

This works:

void main() {
   static auto b() { return bar();}
   auto a = A!b();
}

But of course, it's not ideal, as you are relying on the inliner to make 
this performant. Shortened version (I think this implies static):

auto a = A!(()=>bar())();

Yuck. I really think what you wish should be allowed, even if using some 
syntax to declare you know what you are doing.

I tried some other goofy stuff, but I could not get around the 
requirement that the alias not change the protection of the symbol.

-Steve


More information about the Digitalmars-d mailing list