www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I can't get passing an array by reference to work anymore...

reply TheGag96 <thegag96 gmail.com> writes:
So I'm just doing a small test program here: 
http://pastebin.com/UYf2n6bP

(I'm making sure I know quicksort for my algorithms class, I know 
functionally this won't work as-is)

I'm on Linux, 64-bit, DMD 2.068.1, and when I try to compile this 
I'm getting:

quicksort.d(18): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])
quicksort.d(19): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])

No matter how I attempt to define the array test, it will never 
allow me to pass it by reference no matter what. I even 
copy-pasted some example code from here 
(https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Pointers,_Pass-By-Reference_and_Static_Array
#Pass_By_Reference, bottom of the page), and it gave the same error.

What's the problem here? I SWEAR I've passed arrays by reference 
before just like this. Thanks guys.
Sep 24 2015
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 25 September 2015 at 02:37:22 UTC, TheGag96 wrote:
 So I'm just doing a small test program here: 
 http://pastebin.com/UYf2n6bP

 (I'm making sure I know quicksort for my algorithms class, I 
 know functionally this won't work as-is)

 I'm on Linux, 64-bit, DMD 2.068.1, and when I try to compile 
 this I'm getting:

 quicksort.d(18): Error: function quicksort.quickSort (ref int[] 
 arr) is not callable using argument types (int[])
 quicksort.d(19): Error: function quicksort.quickSort (ref int[] 
 arr) is not callable using argument types (int[])

 No matter how I attempt to define the array test, it will never 
 allow me to pass it by reference no matter what. I even 
 copy-pasted some example code from here 
 (https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Pointers,_Pass-By-Reference_and_Static_Array
#Pass_By_Reference, bottom of the page), and it gave the same error.

 What's the problem here? I SWEAR I've passed arrays by 
 reference before just like this. Thanks guys.
Im not sure why but.. Are you sure that you want to pass the array by ref? In D the type int[] is like struct { size_t length; int* ptr;} which means you are accessing through a pointer and thus changing the data but not reassigning the array as you would with ref. That link may well be out of date, Ali's Book (http://ddili.org/ders/d.en/index.html) is a great resource if you are learning. Alternately look at the std algorithm implementation of quicksort. Nic
Sep 24 2015
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 25 September 2015 at 02:37:22 UTC, TheGag96 wrote:
 What's the problem here? I SWEAR I've passed arrays by 
 reference before just like this. Thanks guys.
I'm seeing the same error, but I haven't yet determined why. At any rate, this works: ``` import std.stdio; void append(ref int[] arr, int val) { arr ~= val; } void main() { auto a1 = [10,20,30]; a1.append(40); writeln(a1); } ``` The compiler error aside, there's a big difference between this function and your quicksort. This one is modifying the structure (or metadata) of the source array. Your quicksort is only dealing with the elements. You don't need to use ref to effect changes on the elements of the source array. arr[1] = 2 on a function parameter will be reflected in the source array even when ref is absent. Only when you need to modify the length of the source array, or cause it to point to a new memory block, would you need to use ref. So in your particular case, you can drop the ref from your parameter and the compiler error will go away. However, the function as written is producing a range violation :) I'd still like to know what's causing the compiler error with ref in this case, though.
Sep 24 2015
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/24/2015 09:14 PM, Mike Parker wrote:

 I'm seeing the same error, but I haven't yet determined why.
It is because rvalues cannot be bound to 'ref' parameters: void quickSort(ref int[] arr) { // ... quickSort(arr[0..wall]); // This slice is rvalue quickSort(arr[wall+1..$]); // ditto } As it has already been said, there is no reason for the ref for this function but the compilation error can be removed by making them lvalues: auto left = arr[0..wall]; // now lvalue quickSort(left); auto right = arr[wall+1..$]; // ditto quickSort(right); Then it will expose another problem with the code. ;) Ali
Sep 24 2015