www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Set null as function array parameter

reply "Oleg" <gaolong i.ua> writes:
Hello. How can I call a function with null as parameter, which I 
don't want to set. For example:
void foo(ref int[] param1) {}
I can't call this function like:
foo(null);
Is it possible to set default value for an array parameter or 
pass null/empty array? I can create empty array and pass it, but 
it looks ugly:
int[] x;
foo(x);

Thanks.
Jan 12 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 12 January 2015 at 15:51:17 UTC, Oleg wrote:
 void foo(ref int[] param1) {}
Why are you using ref? Take that off and you can pass any array, including null, with ease. The only difference is changes to length won't be seen outside the foo function.
Jan 12 2015
parent reply "Oleg" <gaolong i.ua> writes:
On Monday, 12 January 2015 at 15:59:43 UTC, Adam D. Ruppe wrote:
 Why are you using ref? Take that off and you can pass any 
 array, including null, with ease.
Because dynamic arrays are passed by value to functions. Will it make another copy of array, if I'll pass array by value? Looks like it won't (I've checked pointers), but I read in wiki (http://en.wikibooks.org/wiki/D_%28The_Programming_Language%29/d2/Strings_and_Dynamic_Arrays#Passing_Dynamic_ rrays_to_Functions) that it will copy "structure that contains the -pointer to the first element- and the length is copied and passed". Does it mean something else? maybe i don't understand it correctly.
Jan 12 2015
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 12 Jan 2015 16:32:30 +0000
Oleg via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 On Monday, 12 January 2015 at 15:59:43 UTC, Adam D. Ruppe wrote:
 Why are you using ref? Take that off and you can pass any=20
 array, including null, with ease.
=20 Because dynamic arrays are passed by value to functions. Will it=20 make another copy of array, if I'll pass array by value? =20 Looks like it won't (I've checked pointers), but I read in wiki=20 (http://en.wikibooks.org/wiki/D_%28The_Programming_Language%29/d2/Strings=
_and_Dynamic_Arrays#Passing_Dynamic_Arrays_to_Functions)=20
 that it will copy "structure that contains the -pointer to the=20
 first element- and the length is copied and passed". Does it mean=20
 something else? maybe i don't understand it correctly.
nope, it means exactly what is written there. except that dynamic array is represented by struct like this: struct { void *dataptr; size_t itemCount; } this is what D calls "dynamic array", and this is what passed by value: struct with two members. dynamic array contents are *not* a part of "dynamic array type". yes, this is confusing.
Jan 12 2015
parent reply "Oleg" <gaolong i.ua> writes:
On Monday, 12 January 2015 at 16:44:42 UTC, ketmar via 
Digitalmars-d-learn wrote:
 nope, it means exactly what is written there. except that 
 dynamic array
 is represented by struct like this:

   struct {
     void *dataptr;
     size_t itemCount;
   }

 this is what D calls "dynamic array", and this is what passed 
 by value:
 struct with two members. dynamic array contents are *not* a 
 part of
 "dynamic array type".

 yes, this is confusing.
Thank you.
Jan 12 2015
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 12 Jan 2015 16:53:59 +0000
Oleg via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 On Monday, 12 January 2015 at 16:44:42 UTC, ketmar via=20
 Digitalmars-d-learn wrote:
 nope, it means exactly what is written there. except that=20
 dynamic array
 is represented by struct like this:

   struct {
     void *dataptr;
     size_t itemCount;
   }

 this is what D calls "dynamic array", and this is what passed=20
 by value:
 struct with two members. dynamic array contents are *not* a=20
 part of
 "dynamic array type".

 yes, this is confusing.
=20 Thank you.
be careful with chaning dynamic array length though. it won't be seen outside of function, as Adam wrote. and it can cause some side-effects like partially modifyed array (if you modified some array elements and then changes length, which causes reallocing).
Jan 12 2015
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 12 January 2015 at 16:32:31 UTC, Oleg wrote:
 Because dynamic arrays are passed by value to functions. Will 
 it make another copy of array, if I'll pass array by value?
It is important to think of the underlying representation with a pointer and length passed by value. Since it is a pointer, the contents are never copied with a D slice, but reallocation or length changes won't be seen unless it is ref. In general, I'm skeptical of ref arguments btw because they aren't needed as often as they are used. ref arrays, ref pointers, and ref class objects should only be used if you are going to reassign it to something new (or change the length of the array) and want that seen by the called - the contents are automatically passed by reference. ref structs are sometimes useful, but even there I tend to avoid them. Value passing is often faster anyway (if the struct.sizeof <= pointer.sizeof it is a win every time, and even if bigger than that it often is) and generally simpler to follow. So my advice is to break the habit of using ref everywhere in D, you usually don't need it.
Jan 12 2015