Passing function parameters by name

downs default_357-line at yahoo.de
Tue Dec 4 08:43:21 PST 2007


You can "sort-of" do function parameters by name by turning the function into a template, and using some typedefs.

Completely untested example:

>
> typedef int _Foo;
> _Foo Foo(int e) { return cast(_Foo) e; }
>
> typedef int _Bar;
> _Bar Bar(int e) { return cast(_Bar) e; }
>
> void test(T)(T t) {
>   static if (is(T==_Bar)) writefln("Got bar: ", t);
>   static if (is(T==_Foo)) writefln("Got foo: ", t);
> }
>
> void main() {
>   test(Foo=4); test(Bar=17);
> }
>

Multiple parameters can be handled with some templates and static ifs as well. :)
 --downs



More information about the Digitalmars-d mailing list