readonly?

Artur Skawina art.08.09 at gmail.com
Thu Jul 12 03:30:54 PDT 2012


On 07/12/12 01:09, Jonathan M Davis wrote:
> On Wednesday, July 11, 2012 10:56:23 Artur Skawina wrote:
>> Can anybody think of a reason to keep the current (broken) behavior?
> 
> Easily.

You misunderstand the "current (broken) behavior" part - it is about
what 'C*' is, it is not at all about class references.

> Making Object* point to the object itself rather than the reference would be 
> so broken that it's not even funny. I can understand why you would think that 
> such a change should be made, but that's only because you haven't thought 
> through all of the consequences of that. It's all wrapped up in the very 
> reason why Rebindable is in the library rather than being built into the 
> language.
> 
> For starters, if you have
> 
> class C {}
> C c;
> 
> the type of c is C. C is a reference to the class named C. It does _not_ mean 
> the same thing as the C in the class declaration at all. If it were a struct 
> rather than a class, it would be equivalent to C* (save for the fact that a 
> reference and a pointer aren't quite the same). So, _everywhere_ in the type 
> system that you see C, it's really C*, _not_ C as in the class C. There is _no 
> way_ in the type system to refer to the class itself. So, making C* be a 
> pointer to the object rather than the reference isn't even representable in 
> the type system.

Of course it is - you even say it above: "_everywhere_ in the type system that
you see C, it's really C*".
But instead of considering the implications and required semantics, you are 
making several assumptions, which are wrong.
For example - let 'C' be a class, then: 'new C' must return a reference to C,
not a pointer; dereferencing a 'C*' pointer should of course not be valid.


> There are all kinds of practical side effects for this. For instance, if you 
> had
> 
> class D : C {}
> 
> C c;
> C* cPtr = &c;
> 
> and C* was a pointer to the class itself, then it would almost be like doing
> 
> C* c;
> C* cPtr = &c;
> 
> which makes no sense at all.

This is actually what effectively happens with the *current* model, it in deed
makes little sense, and is the reason for my "why?" questions...


> It also introduces object slicing - as in the 
> type of slicing that C++ has when you assign a derived object to a base class 
> object which is on the stack. In C++,
> 
> D d;
> C c = d;
> 
> results in the D portion of d being chopped off, potentially putting it in an 

D and C (in D) are references - there's no problem. I have no idea why you think
classes would be value types.

> invalid state, and almost certainly isn't what you wanted to do. D avoids this 
> by requiring that polymorphic objects (classes) be on the heap. But if C* 
> referred to the object itself, it becomes a probem again.
> 
> C c = new C;
> C* cPtr = &c;
> *cPtr = new D;
> 
> Since cPtr was a pointer to the object, *cPtr would be the object itself, and 
> so the D object would be assigned to the C object and get sliced, just like in 
> the C++ example.

Dereferencing a class pointer has to be illegal, i thought that was obvious and
not needed to be spelled out. It's a simple and easily understandable rule.
So '*cPtr' wouldn't be valid - there is no problem.

> In additon, making Object* be a pointer to the object itself would make 
> dealing with pointers to local objects _very_ inconsistent. Take this for 
> example
> 
> void func(T)(T* t, T value)
> {
>  *t = value;
> }
> 
> int i;
> func(&i, 7);
> 
> C c = getC();
> func(&c, new C);
> 
> When func is called with &i, it sets i to 7. But when it's called with &c, it 
> doesn't set c. It sets what c refers to. This means that instead of changing 

'*t' would not compile for a T*==C* (when the latter means a direct
pointer to the instance) ; see above.

Whether that should compile, in the model i gave as an example, is a
different question - there the call is 'func(C**, C)' which results in
'C* = implicit_cast(C*)C' -- this modifies the reference. I can see
arguments for disallowing this, but then the 'rebindable' problem would
be back, so I'd lean toward keeping it legal.

> the local variable c, you've changed an object on the heap which other 
> references could refer to, and now instead of just affecting the local 
> reference, _every_ reference is affected. This is _completely_ different from 
> how it works with &i. It's more in like with how it would work if you had
> 
> int* iPtr = getIntPtr();
> func(&iPtr, new int);
> 
> and since C is essentially equivalent to C*, it would then be impossible to 
> pass the reference itself to func to be set.

That's why I have '&C_instance' result in 'C**'. not 'C*'. Yes, it means
classes are treated differently -- but they *already* are. Yes, it's *only*
about the type, most (ie all desirable) semantics of references are kept. 

> It would also make it so that taking a pointer for a parameter was very 
> different from taking ref or out for classes, when it's nearly identical for 
> everything else.
> 
> void refFunc(T)(ref T t, T value)
> {
>  t = value;
> }
> 
> int i;
> refFunc(i, 7);
> 
> C c = getC();
> refFunc(c, new C);
> 
> With your suggestion, this code operates identically for i but does something 
> completely different for c. Now, instead of setting the object, it's setting 
> the reference.

Huh? There are no (explicit) pointers involved here, there's no difference.
I think you didn't actually read my example carefully enough, as you keep
bringing up things that nobody suggested, and which wouldn't make any sense.

> AAs would also be very broken if Object* pointed to the object rather than the 
> reference. Take this code:
> 
> int[string] aa;
> int* intPtr = "hello" in aa;
> 
> C[string] bb;
> C* cPtr = "hello" in bb;
> 
> With aa, you get a pointer to the value which is at the key "hello". With bb, 
> you get a pointer to the object which the value at "hello" refers to. So, once 
> again, setting *cPtr sets the object rather than the reference, and slicing 
> becomes a problem. On top of that, what happens with null? Right now, you can 
> do
> 
> bb["hello"] = null;
> C* v1 = "hello" in bb;
> C* v2 = "world" in bb;
> 
> The fact that v1 is non-null tells you that "hello" is in bb, and the fact 
> that v2 is null tells you that "world" isn't in bb. You can then dereference 
> v1 and get at the value which is at "hello", which is null. But what happens 
> when C* nows points to the object rather than the reference? It becomes 
> impossible to distinguish between the case when the key isn't in the AA and 
> when the value at that key is null. You could fix that by making it so that
> 
> C[string] bb;
> 
> was implicitly
> 
> C*[string] bb;
> 
> but then the type that in returns would have to be C**, making it so that 
> Objects behaved differently with AAs than every other type, since in all other 
> cases with T[U], in returns T*, not T**.

The AA 'in' operator is one of the odder ones, but there's no problem.
Remember the '&C => C**' rule? 'typeof("abc" in bb)==C**'. Maybe it's
easier to understand when you realize that what happens underneath is
equivalent to declaring a 'C*[string] bb'.

> (within the type system) to refer to the object itself, so the behavior of &c 
> is _completely_ consistent with the rest of the language.

Nope.

But the question was if there is any advantage to the current model? And so
far I've seen none mentioned.

Note that one alternative is to basically treat 'C*' similarly to a reference
to C, which means there are no slicing or polymorphism issues, as the obvious
extra restrictions on such a type take care of things. This new model would be
much saner, while allowing things that are currently not (cleanly) possible.

I'm really just wondering if there was some real reason behind the design, or
if it was just an accident, because nobody considered the consequences - like
in the case of so many other D features.
This isn't about changing things now (at least not short term), it's about
future directions. 

artur


More information about the Digitalmars-d-learn mailing list