| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
May 17, 2013 how to have alias this with an unaccessible member? | ||||
|---|---|---|---|---|
| ||||
Attachments:
| How to have alias this with an unaccessible member (x below).
Making the member private won't work as it'll disable all operations on
said member.
----
struct A(T){
T x;
//private T x would prevent alias this from doing anything useful
alias x this;
}
void main(){
auto a=A!int;
a++;//should do a.x++;
static assert(!__traits(compiles,a.x)); // I want this to hold
}
----
| |||
May 18, 2013 Re: how to have alias this with an unaccessible member? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | Will this do?
--------------------
struct A(T)
{
private T x;
ref T get()
{
return x;
}
alias get this;
}
---------------------
| |||
May 18, 2013 Re: how to have alias this with an unaccessible member? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | Btw, fun fact. This code crashes 2.063 beta:
------------------------
struct A(T)
{
private T x;
alias y = x;
alias y this;
}
------------------------
dmd: aliasthis.c:114: virtual void AliasThis::semantic(Scope*): Assertion `t' failed.
| |||
May 24, 2013 Re: how to have alias this with an unaccessible member? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot Attachments:
| This won't do for the same reason: now 'get' is made public so we're back to the same problem (inverting roles of x and get).
However what about changing the behavior of alias this as follows:
when a member/method x is private, "alias x this" behaves as if x was not declared private.
I think this makes sense:
* this allows protection (x is an implementation detail) so that 'this'
behaves exactly as 'x'
* also, without this change of behavior, "alias x this" would not make any
sense in terms of behavior outside the class (inside behavior should just
access x directly)
Then, when multiple alias this statements will become allowed in D, we would have implemented the same concept as "embedding" in GO.
Any thoughts?
here's what we would have:
----
struct A(T){
private T x would prevent alias this from doing anything useful
alias x this;
}
void main(){
auto a=A!int;
a++;//should do a.x++; //semantic change: even though x is private, all
its methods are un-privated through alias this.
static assert(!__traits(compiles,a.x));
}
----
On Sat, May 18, 2013 at 1:33 AM, Dicebot <m.strashun@gmail.com> wrote:
> Will this do?
>
> --------------------
>
>
> struct A(T)
> {
> private T x;
>
> ref T get()
>
> {
> return x;
> }
>
> alias get this;
> }
>
> ---------------------
>
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply