readonly?
Artur Skawina
art.08.09 at gmail.com
Wed Jul 11 14:09:17 PDT 2012
On 07/11/12 17:54, David Nadlinger wrote:
> On Wednesday, 11 July 2012 at 10:05:40 UTC, Artur Skawina wrote:
>> Because it doesn't let you have a real pointer to a class.
>
> What is a »real pointer«? Class references are really just pointers, in a way – you can cast them to void*.
A "real pointer" is a pointer.
Class refs are basically pointers, but with some extra limitations and syntax
sugar. The key distinction is that you can implement references on top of
pointers, but the opposite isn't true. Hence refs are not "real" pointers.
Casting refs in order to extract their value /is/ possible, but that gets
ugly and dangerous (if not actually 'undefined'); the ClassPtr type that
i posted in this thread is a good example - it may be just ~6 lines of code,
but that's six line of code that everybody that's reading or using it must
analyze, before determining if it's correct and sane. (and, yes, it's not
perfect, eg it doesn't even try to handle immutable - simply because I had
no need for that so far)
>> The obvious alternative would be:
>>
>> auto r = new Bar(); // reference
>> Bar* p = r; // pointer to Bar; ref implicitly converts to pointer.
>> auto pr = &r; // typeof(pr)==Bar** ; can't do better w/o ref types.
>>
>> So, does the current scheme have any advantages?
>
> When discussing a language change, the question should always be: Does the _new_ scheme have any advantages?
Yeah, except when the old scheme was an accident, which I have to assume is
the case here. Hence the questions, just in case the behavior was in fact
chosen deliberately and I'm missing something.
The advantages of having pointers to classes? Eg solving the problem that
triggered this thread w/o hacks like ClassPtr (Rebindable is an even worse
hack). [1]
Not to mention that the current model is extremely misleading; consider that
three different people got it wrong in this thread alone...
artur
[1] "direct" pointers, going via a ref obviously works, even if less efficient.
Which brings us back to the question - are there any advantages of /that/ scheme?
More information about the Digitalmars-d-learn
mailing list