March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SpookyET | In article <opr36979x51s9n15@saturn>, SpookyET says...
>
>Damn, you still have to mess with that C crappy way.
>
>This way (C# style), it would be much better:
>
>void main()
>{
> printNumbers(1, 2, 3, 4, 5);
>}
>
>void printNumbers(params int[] list)
>{
> foreach(int number; list)
> {
> puts(number);
> }
>}
>
>The compiler can create that array for you.
>"params" will always have to be at the end of the parameters list, which
>is a clue for the compiler to create an array and put the the rest of the
>arguments in it.
>
>void main()
>{
> printStuff("Hello World!", 'D', 1, 2, 3, 4, 5);
>}
>
>void printStuff(char[] message, char letter, params int[] list)
>{
> puts(message);
> puts(letter);
> foreach(int number; list)
> {
> puts(number);
> }
>}
>
>--
>Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
I think that's a good, but there's a problem: what if you want to receive
parameters of any type? In C# you can do 'params object[] list', but in D you
wouldn't be able, because D doesn't do boxing/unboxing like C#.
It's been talked a lot, but there doesn't seem to be a sensible way to do it.
-------------------
Carlos Santander B.
|
March 02, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote:
> I think that's a good, but there's a problem: what if you want to receive
> parameters of any type? In C# you can do 'params object[] list', but in D you
> wouldn't be able, because D doesn't do boxing/unboxing like C#.
> It's been talked a lot, but there doesn't seem to be a sensible way to do it.
I think i know how it can be implemented. But i'd say it should be left to post-release D.
And yes, as to your other post, i really meant that i would like dynamic creation of arrays, but it's not there yet. And i'm not sure Walter was eager to do it. Maybe i come to hack the compiler as well, but now i really need a rest. Sorry for not keeping my promise.
-eye
|
March 02, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | sorry for the self-reply, but here goes a better version: // this part should go into phobos somewhere private import std.c.stdio; template va_array(T) { T[] va_array(uint* ptr) { T* args = cast(T*)(cast(va_list)ptr + uint.size); return args[0 .. *ptr]; } } // this part is what user code would look like void test(uint n,...) { int[] args = va_array!(int)(&n); printf("last element is %d\n", args[args.length-1]); } class TestClass { void test2(uint n,...) { double[] args = va_array!(double)(&n); printf("last element is %f\n", args[args.length-1]); } void test3(uint n,...) { char[][] args = va_array!(char[])(&n); printf("last element is %.*s\n", args[args.length-1]); } } int main() { test(5, 1, 2, 3, 4, 5); TestClass x = new TestClass(); x.test2(5, 1.1, 2.2, 3.3, 4.4, 5.5); x.test3(2, "hello", "world"); return 0; } |
March 02, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | On Mon, 1 Mar 2004 23:45:15 +0000 (UTC) (03/02/04 10:45:15) , Carlos Santander B. <Carlos_member@pathlink.com> wrote: > In article <opr37gm5ti1s9n15@saturn>, SpookyET says... >> >> printNumbers([1, 2, 3, 4, 5]); Does't work >> >> -- >> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ > > No, D doesn't let you create arrays on the fly (that's the way it's said, > right?). > <FirstReaction> How dumb is that!? </FirstReaction> <SecondReaction> See 1st reaction. </SecondReaction> Surely this is just one of those things that hasn't *yet* made it to the language. A literal array is a valid thing to do. It is used often when available. Please excuse my lack of D experience and my mentioning another language I am familiar with, but in Euphoria it could be coded thus ... procedure printNumbers(sequence list) for i = 1 to length(list) do ConsoleOut(list[i]) end for end procedure printNumbers( {1, 2, 3, 4, 5 } ) In Euphoria, a sequence is a list of zero or more 'things', referenced by a 1-based integer index. The 'things' can be any datatype, which by the way are only integers, atoms ( read: floats ), or sequences. A character string is just a sequence of integers. Euphoria handles all garbage collection. Parameters can only be passed by value (in effect). There is no pointer/reference/address-of concept.... ...ooops...but this is probably too much information, right? ;-) Sorry, got carried away. -- Derek |
March 02, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SpookyET | >Damn, you still have to mess with that C crappy way. Right. > >This way (C# style), it would be much better: *arg*, please stop it! C# is a crapy language. I like other languages better than D, but have you ever seen me posting: In my Favorite languge this is done better. And that, too. ... >void main() >{ > printNumbers(1, 2, 3, 4, 5); >} > >void printNumbers(params int[] list) >{ > foreach(int number; list) > { > puts(number); > } >} > >The compiler can create that array for you. Wouldn't array literals be a much better alternative? It would be as easy, but could be used in other situations, too. And you should allways prefer the more flexible way of doing something if it isn't more complicated. >"params" will always have to be at the end of the parameters list, which is a clue for the compiler to create an array and put the the rest of the arguments in it. With array-literals you wouldn't have this restriction. And I don't like being restricted. C# made some steps in the right directions, but never goes far enough. It offers you some helpfull things that can be used in common cases, but in not that common cases you have a big problem. D already is much better than C#. |
March 02, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | "Ilya Minkov" <minkov@cs.tum.edu> wrote in message news:c20ivv$2j5k$1@digitaldaemon.com... | Carlos Santander B. wrote: | | > I think that's a good, but there's a problem: what if you want to receive | > parameters of any type? In C# you can do 'params object[] list', but in D you | > wouldn't be able, because D doesn't do boxing/unboxing like C#. | > It's been talked a lot, but there doesn't seem to be a sensible way to do it. | | I think i know how it can be implemented. But i'd say it should be left | to post-release D. | | And yes, as to your other post, i really meant that i would like dynamic | creation of arrays, but it's not there yet. And i'm not sure Walter was | eager to do it. Maybe i come to hack the compiler as well, but now i | really need a rest. Sorry for not keeping my promise. | | -eye I hope I'm not beating a dead horse with workarounds until array literals get supported but here's yet more code for those who want something that works today: private import std.c.stdio; template va_array(T) { T[] va_array(uint* ptr) { T* args = cast(T*)(cast(va_list)ptr + uint.size); return args[0 .. *ptr]; } } template new_array(T) { T[] new_array(uint n,...) { return va_array!(T)(&n).dup; } } // user code void test(int[] x) { printf("last element: %d\n",x[x.length-1]); } int main() { test(new_array!(int)(3, 1,2,3)); return 0; } |
Copyright © 1999-2021 by the D Language Foundation