Passing function parameters by name

Robert Fraser fraserofthenight at gmail.com
Tue Dec 4 11:53:53 PST 2007


downs wrote:
> 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

Doing that for every function would be ugly & error-prone, though (even 
with the magic of nested templates/tuples/mixins).



More information about the Digitalmars-d mailing list