Re: I can't get passing an array by reference to work anymore...
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
Re: I can't get passing an array by reference to work anymore...
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.
Re: I can't get passing an array by reference to work anymore...
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_Arrays#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
I can't get passing an array by reference to work anymore...
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_Arrays#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.
Re: Passing an array by reference?
On Thursday, 24 July 2014 at 16:15:44 UTC, John Colvin wrote: On Thursday, 24 July 2014 at 16:06:00 UTC, Rishub Nagpal wrote: 1 class Test 2 { 3 int[][] array; 4 this(ref int[][] d) 5 { 6 array = d; 7 } 8 9 } 10 11 void main() 12 { 13 Test t = new Test([[1,1],[1,1]]); //does not compile 14 } 15 what is the best way to pass a literal ([[1,2],[3,4]]) by reference? Is using ref the best way? I am still a bit fuzzy with in/out and scope Why do you need to pass the array by reference? D's slices are just windows on to memory, they don't copy the contents when you pass them around. Anyway, if you do need ref, the problem is that [[1,1],[1,1]] is a temporary, an rvalue, and ref can't take rvalues. Ah I did not know that. jwhear on IRC cleared it up for me http://dpaste.dzfl.pl/c7ae7e71d45c
Re: Passing an array by reference?
On Thursday, 24 July 2014 at 16:06:00 UTC, Rishub Nagpal wrote: 1 class Test 2 { 3 int[][] array; 4 this(ref int[][] d) 5 { 6 array = d; 7 } 8 9 } 10 11 void main() 12 { 13 Test t = new Test([[1,1],[1,1]]); //does not compile 14 } 15 what is the best way to pass a literal ([[1,2],[3,4]]) by reference? Is using ref the best way? I am still a bit fuzzy with in/out and scope Why do you need to pass the array by reference? D's slices are just windows on to memory, they don't copy the contents when you pass them around. Anyway, if you do need ref, the problem is that [[1,1],[1,1]] is a temporary, an rvalue, and ref can't take rvalues.
Passing an array by reference?
1 class Test 2 { 3 int[][] array; 4 this(ref int[][] d) 5 { 6 array = d; 7 } 8 9 } 10 11 void main() 12 { 13 Test t = new Test([[1,1],[1,1]]); //does not compile 14 } 15 what is the best way to pass a literal ([[1,2],[3,4]]) by reference? Is using ref the best way? I am still a bit fuzzy with in/out and scope