public aliases to private/package symbols

Nick Sabalausky a at a.a
Tue Jan 24 17:23:41 PST 2012


"Peter Alexander" <peter.alexander.au at gmail.com> wrote in message 
news:jzvidktaqmkptrooimvt at dfeed.kimsufi.thecybershadow.net...
> On Wednesday, 25 January 2012 at 00:08:25 UTC, Timon Gehr wrote:
>> Accessibility-raising aliases are trivially safe, because the alias 
>> declaration must have access to the aliased symbol.
>
> You are probably right about not introducing holes, but I can imagine this 
> getting tricky, and perhaps confusing in some cases. Here are some off the 
> top of my head.
>
> module A;
> private class Foo {}
> public alias Foo Bar;
>
> In other modules that import A:
>
> Bar b = new typeof(b)(); // typeof(b) is Foo. Is this allowed?
>
> T foo(T)(T x) { return new T(); }
> Bar b;
> b = foo(b); // T is deduced to Foo, should this work?
>
> Bar b = new Bar();
> mixin("b = new " ~ typeof(b).stringof ~ "();"); // This fails, the string 
> is "Foo"
>

In all the above, the type *should* be Bar, not Foo (although it might not 
currently be - I'd consider that something that needs to be changed). Bar 
should refer to Foo *behind the scenes*, and as such, the fact that it 
refers to something else behind the scenes is totally irrelevent.

This would also seem to be related to issue of error messages currently 
referring to the underlying type insted of the aliased type (which I'm 
convinced is the wrong way round).

>
> Just thinking about this has made me change my position. Accessibility 
> raising aliases should not be allowed. It's a minefield.

The issue is not with aliases, accessability already has a natural grey 
area:

====================
module lib;

private struct Foo {}

// Should any of these be allowed?
public Foo getFoo() { return Foo(); }
public void takeFoo(Foo f) {}
struct Bar
{
    Foo f;
}

------

module main;
import lib;

getFoo();  // Allowed?

takeFoo(getFoo());  // Allowed?

Bar b;  // Allowed?

takeFoo(b.f);   // Allowed?

b.f = getFoo();   // Allowed?

// Allowed? If so, can you *do* anything with x?
auto x = getFoo();

====================




More information about the Digitalmars-d mailing list