www.digitalmars.com         C & C++   DMDScript  

D - varargs with void[]

reply "C. Sauls" <ibisbasenji yahoo.com> writes:
I remember vaguely Walter mentioning the use of void[] in D to 
facilitate the same ability as void* in C... could a little syntax sugar 
be devised to support variable parameter lists using void[]?  Maybe 
something like:

void myprintf(char[] format, void[][]  data) {
   int idx;
   char fc;
   char[] out;
   // ...
   switch (fc) {
     case 'd':
       out ~= .toString(cast(int) data[idx++]);
       break;

     // ...

     case 's':
       out ~= .toString(cast(char[]) data[idx++]);
       break;

     // ...
   }
   // ...
}

Just a thought.

-C. Sauls
-Invironz
Mar 28 2004
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
Just to see if this would technically work at all, I tossed together 
this little experiment, with (IMHO) pretty good results.  Please excuse 
the extrememly rudimentary print() function.  :)

[Source code attached]

-C. Sauls
-Invironz
Mar 28 2004
parent reply Ben Hinkle <bhinkle4 juno.com> writes:
On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com>
wrote:

Just to see if this would technically work at all, I tossed together 
this little experiment, with (IMHO) pretty good results.  Please excuse 
the extrememly rudimentary print() function.  :)
I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
[Source code attached]

-C. Sauls
-Invironz
Mar 28 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Ben Hinkle wrote:

On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com>
wrote:

  

Just to see if this would technically work at all, I tossed together 
this little experiment, with (IMHO) pretty good results.  Please excuse 
the extrememly rudimentary print() function.  :)
    
I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Why not simply make these things pointers? -- -Anderson: http://badmama.com.au/~anderson/
Mar 28 2004
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
If you mean to make data of type void*[] instead of void[][] that won't 
work... I tried it.  Cannot (apparently) cast a char[] or a struct to 
void*, but can cast anything to void[] (with varying degrees of success).

-C. Sauls
-Invironz

J Anderson wrote:
 Ben Hinkle wrote:
 
 On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com>
 wrote:

  

 Just to see if this would technically work at all, I tossed together 
 this little experiment, with (IMHO) pretty good results.  Please 
 excuse the extrememly rudimentary print() function.  :)
   
I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Why not simply make these things pointers?
Mar 29 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
C. Sauls wrote:

 If you mean to make data of type void*[] instead of void[][] that 
 won't work... I tried it.  Cannot (apparently) cast a char[] or a 
 struct to void*, but can cast anything to void[] (with varying degrees 
 of success).

 -C. Sauls
 -Invironz
What about? char [] string; void *tt = (void*)&string[0]; //Looses length void *t = (void*)string; struct a { } a A; void *t = (void*)&A; What do you mean?
 J Anderson wrote:

 Ben Hinkle wrote:

 On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com>
 wrote:

  

 Just to see if this would technically work at all, I tossed 
 together this little experiment, with (IMHO) pretty good results.  
 Please excuse the extrememly rudimentary print() function.  :)
   
I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
Why not simply make these things pointers?
-- -Anderson: http://badmama.com.au/~anderson/
Mar 29 2004
prev sibling parent "C. Sauls" <ibisbasenji yahoo.com> writes:
Just tried rigging up a struct with two int fields and a char[] field. 
The ints work jut fine, the char[] is lost, so you have a point.

-C. Sauls
-Invironz

Ben Hinkle wrote:
 On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji yahoo.com>
 wrote:
 
 
Just to see if this would technically work at all, I tossed together 
this little experiment, with (IMHO) pretty good results.  Please excuse 
the extrememly rudimentary print() function.  :)
I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*).
[Source code attached]

-C. Sauls
-Invironz
Mar 29 2004