www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Implicit array conversions - something rotten

reply Mike Capp <mike.capp gmail.com> writes:
I was reading the "Implicit Conversions" section of arrays and did a
double-take; several of the conversions listed looked flat-out wrong. A bit of
experimentation muddied the waters further - several documented conversions
don't work, and several documented conversions that I was convinced had to be
typos do. 

Most importantly, D really will implicitly cast from a Derived[] to a Base[].
This is a huge hole in type-safety - it lets you put an object of ANY class into
an array of ANY class type without ever writing an explicit cast. I don't know
of any other statically-typed OO language that allows it.

#class Base {}
#class Derived : Base {}

#void main()





















































Aug 07 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Mike Capp" <mike.capp gmail.com> wrote in message 
news:dd5v4m$vk2$1 digitaldaemon.com...
I was reading the "Implicit Conversions" section of arrays and did a
 double-take; several of the conversions listed looked flat-out wrong. A 
 bit of
 experimentation muddied the waters further - several documented 
 conversions
 don't work, and several documented conversions that I was convinced had to 
 be
 typos do.

 Most importantly, D really will implicitly cast from a Derived[] to a 
 Base[].
 This is a huge hole in type-safety - it lets you put an object of ANY 
 class into
 an array of ANY class type without ever writing an explicit cast.
I agree http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/3681
 I don't know
 of any other statically-typed OO language that allows it.
C++ allows it because C++ is C++ (by which I mean arrays are pointers). assigning into the array to make sure the the array type is compatible with the element being inserted.
 #class Base {}
 #class Derived : Base {}

 #void main()






















































 
Aug 07 2005
parent Mike Capp <mike.capp gmail.com> writes:
In article <dd625n$11rs$1 digitaldaemon.com>, Ben Hinkle says...
"Mike Capp" <mike.capp gmail.com> wrote in message 
news:dd5v4m$vk2$1 digitaldaemon.com...
 Most importantly, D really will implicitly cast from a Derived[] to a 
 Base[]. [...] I don't know
 of any other statically-typed OO language that allows it.
C++ allows it because C++ is C++ (by which I mean arrays are pointers).
But D uses reference semantics for all objects of class type, so Object[] in D corresponds to Object** in C++, not to Object*. And there's no implicit Derived** to Base** conversion in C++. cheers Mike
Aug 07 2005