May 05, 2004 Re: Typesafe variable arguments proposal | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c76q23$12h5$1@digitaldaemon.com... > Achilleas Margaritis wrote: > > >"J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c75c6f$1sdd$1@digitaldaemon.com... > > > > > >>Just starting the type safe variable arguments argument :) up again. > >> > >>I proposed something like this ages ago, I've had a bit of time to reflect and so I'll propose it again. > >> > >>Why not make variable arguments behave like templates: > >> > >>template printT(T) > >>{ > >> void print(T t) > >> { > >> > >> } > >>} > >> > >> > >>void print( ... data) > >>{ > >> foreach (... type; data) > >> { > >> printT!(typeof(data)).print(data); > >> } > >>} > >> > >>//Note you could change the ... to something else for inner function part. > >> > >>Say I wrote something like: > >> > >>int a; > >>char [] b; > >> > >>print(a, b); > >> > >>The compiler would produce a function like from the print template above, where data is a constant array. > >> > >>void print(int data[0], char [] data[1]) //Of course arrays like this > >>aren't possible at the moment > >>{ > >> foreach (... type; data) //Note this loop could be unrolled if the > >>compiler felt like it. > >> { > >> printT!(typeof(data)).print(data); > >> } > >>} > >> > >>Also, it should be possible to specialize these functions (by overloading them, with the exact parameters). > >> > >>If you wanted to do the switch thing you could do it with if's like (which would easily be optimised): > >> > >> template nameof(T : bit) { char [] name() { return "bit"; } } > >> template nameof(T : byte) { char [] name() { return "byte"; } } > >> > >>void print( ... data) > >>{ > >> foreach (... type; data) > >> { > >> if (nameof(data) == "bit") > >> { > >> //... > >> } > >> else if (nameof(data) == "byte") > >> { > >> //... > >> } > >> } > >>} > >> > >>But I wouldn't recommend it. > >> > >>It would also be useful to write templates with ... arguments: > >> > >>template funcT(... T) > >>{ > >> void func(T data) > >> { > >> //... > >> } > >>} > >> > >>alias funcT(int, char[]).func func; > >> > >>-- > >>-Anderson: http://badmama.com.au/~anderson/ > >> > >> > > > >I like the variant implementation better. > > > > > > > > > > > You could also do switch statements with my proposal. Another difference I see is mine allows for a more abstract level of programming. > > template nameof(T : bit) { char [] name() { return "bit"; } } //Would > would probably be in the std/language eventually > template nameof(T : byte) { char [] name() { return "byte"; } } > > void print( ... data) > { > foreach (... type; data) > { > switch (nameof(data)) > { > case "bit" > break; > case "byte" > break; > } > } > } > > I still don't recommend the switch way of doing things. It's non-pluginable and it just looks like reflection to me. It is reflection. |
May 05, 2004 Re: Typesafe variable arguments proposal | ||||
---|---|---|---|---|
| ||||
Posted in reply to Achilleas Margaritis | Achilleas Margaritis wrote: >I don't think efficiency plays any role here. It is not that printf is used >in code where efficiency matters. > >On the other hand, code size matters. Imagine a program where there are lots >of trace output: each different trace will be a different function. And >there may be thousands (for example, one of the applications in the place I >work, it is 120,000 lines of code, and it has over 10,000 TRACE lines; it is >a DIS simulation with lots of custom protocols used in radars). > > > I don't buy that, not for function arguments. You can already write your own dynamic variable arrays. That is something that could be in stl. Besides even D is able to make use of repetitive codes in templates now. The compiler should be able to decide if it is going to expand (roll-out) the function or not. It's the static type checking that is most important. -- -Anderson: http://badmama.com.au/~anderson/ |
Copyright © 1999-2021 by the D Language Foundation