Pure dynamic casts?

Daniel Keep daniel.keep.lists at gmail.com
Tue Sep 22 20:19:10 PDT 2009



Rainer Deyke wrote:
> Jarrett Billingsley wrote:
>> Dynamic downcasts are not pure.
> 
> Actually they are.  Given an object 'o', 'cast(T)(<reference to o>)'
> always returns the same value (either a reference to 'o' or 'null',
> depending on class of 'o' and the value of 'T').
> 
> If the reference to 'o' is changed to point to a different object, then
> the dynamic cast can return a different value.  This does not make the
> dynamic cast impure, since you are passing in a different argument.

On the basis of how I understand pure to be implemented in D at the
moment, it is impure.  Purity only considers the bits passed on the
stack.  If the reference points to the same location in memory, it's
considered the same argument.

The only way to do what you expect would be to compare not just the
arguments, but all memory pointed to by them.  If you've got cyclic
references, you're just plain screwed.

> If the object 'o' is destroyed and another object is created at the same
> physical memory location, 'cast(T)(<reference to o>)' can return a
> different value.  This does not make the dynamic cast impure, since you
> are passing in a (logically) different argument (which happens to use
> the same physical representation).

Logically yes, but in terms of how it's actually implemented, no.



More information about the Digitalmars-d mailing list