Passing dynamic arrays

spir denis.spir at gmail.com
Tue Nov 30 03:28:05 PST 2010


On Mon, 29 Nov 2010 09:13:17 -0500
"Steven Schveighoffer" <schveiguy at yahoo.com> wrote:

> A class is a reference type.  Every operation on a class instance operates  
> on all aliases to that class, only assignment to another class instance of  
> the same type decouples it.
> 
> Consider these two statements:
> 
> "a class is a reference type"
> 
> class C
> {
>     int length;
> }
> 
> foo(C c)
> {
>    c.length = 5; // because a class is a reference type, this affects the  
> original, right?
> }
> 
> "an array is a reference type"
> 
> foo(int[] arr)
> {
>     arr.length = 5; // because an array is a reference type, this affects  
> the original, right?  No?!!!
> }
> 
> This is the major confusion that I think people see.  I would say people  
> assume a "reference type" means that the reference's members are all  
> shared between all aliases.  For an array, it is one level removed, and  
> that confuses the hell out of people.  But the power gained by doing D's  
> way is worth way waaaay more than confusing some noobs.
> 
> Note that many people are used to object-based languages such as C# and  
> Java where arrays actually *are* class instances, and therefore full  
> reference types.

Very good explanation of the mental issue for newcomers. Note that this does not apply only to people coming from languages similar to Java or C#, but also from most, if not all, dynamic languages (including one that are not "officially" OO, like Lua).

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list