alias this private?

js.mdnq js_adddot+mdng at gmail.com
Sun Dec 9 22:43:20 PST 2012


On Monday, 10 December 2012 at 04:50:17 UTC, Ali Çehreli wrote:
> On 12/09/2012 11:52 AM, js.mdnq wrote:
>
> > Well, I would not say they are exactly the same.
>
> Do they have to be exactly the same? You are showing a method 
> that fails to satisfy a requirement, I show another method 
> which according to fail to satisfy another requirement. 
> Software is engineering as well. What is the problem? Let's 
> just solve it.
>
> > In your case you are
> > aliasing on opGet while I'm talking about aliasing on the
> struct itself.
>
> I know.
>
> > e.g., there might be other members you do want the user to
> have access
> > too.
>
> Then I give access to those members.
>
> > With your's, they won't be able to access them(your's is
> actually
> > more restrictive).
>
> I don't see that.
>
> > For example, how can they access a "Validate" method like I
> have using
> > your struct?
>
> It is so trivial that I am beginning to think I have not 
> understood a tiny bit of what you have been saying, but here it 
> goes: :)
>
> struct sPassword
> {
> private:
>     string Password_;
>
> public:
>
>     alias opGet this;
>
>     void opAssign(string value)
>     {
>         Password_ = value;
>     }
>
>     string opGet() { return "******"; }
>
>     bool Validate(string pass) { return pass == Password_; }
> }
>
> void main()
> {
>     sPassword myPass;
>     myPass = "hello";
>     assert(myPass == "******");
>     assert(myPass.Validate("hello"));    // <-- here
> }
>
> I hope others can show me what I've been misunderstanding. :-/
>
> Ali

Na, it seems that my lack of knowing D is at fault. I thought 
when you did alias this it essentially rewrote the object as the 
type... it seems that is not the case, at least, in what you have 
shown. Maybe it simply due to using opGet, which I did not know 
about until you brought it up?

I thought `alias this` essentially treats the object as the alias.

struct A {
     alias value this;
     int value;
     void func();
}

A a;

then a is essentially the same as a.value?  a.func() would be 
a.value.func() which makes no sense? At least that is how I 
thought alias this worked? (it's obviously more complex than what 
I thought)

(I'm new to D so you'll have to forgive all the stupid questions 
;) D seems quite powerful and many useful ways to do things but I 
still haven't wrapped my head around all the intricacies)







More information about the Digitalmars-d-learn mailing list