What is the reasoning behind the lack of conversions when passing parameters

monkyyy crazymonkyyy at gmail.com
Sat Feb 3 02:18:05 UTC 2024


On Friday, 2 February 2024 at 21:16:33 UTC, Carl Sturtivant wrote:
> 

I think its mostly for overloads
You can write a small piece of code that will attempt to conv 
anything to a function parameter

```d
import std.traits;
template polymorphic(alias F){
     auto polymorphic(T...)(T args){
         mixin((){
             import std.conv:to;
             string o;
             foreach(i,S;(Parameters!F)[0..T.length]){
                 o~="args["~i.to!string~"].to!"~S.stringof~",";
             }
             return "return F("~o~");";
         }());
}}

void foo(int,float,bool=true){}
alias bar=polymorphic!foo;
struct myint{
     int i;
     int to(T:int)()=>i;
}
alias somefloat=double;
float to(T:float)(somefloat)=>float.init;

int add(int i,int j)=>i+j;
alias add_=polymorphic!add;
void main(){
     bar(myint(),somefloat.init);
     import std;
     int i=add_(myint(1),myint(2));
     i.writeln;
}
```


More information about the Digitalmars-d mailing list