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

Dukc ajieskola at gmail.com
Mon Feb 19 18:50:45 UTC 2024


On Saturday, 3 February 2024 at 03:19:00 UTC, Walter Bright wrote:
> C++ has this, implicit conversion of structs. It looks great 
> with simple cases. However, as overloading gets more complex, 
> it becomes a source of mass confusion. I've had many C++ 
> programmers tell me that nobody actually knows how C++ 
> overloading works - they just try random things until they get 
> the result they want. There was a lot of talk in the 90s about 
> implicit conversion being a mistake, but by then it was too 
> late and C++ has had to learn to live with it.

There's another way: Template parameter deduction. Just like IFTI 
infers template types from function call parameters, we could 
have a rule that if an eponymous template is assigned to 
something which has unambiquous type, that type is inferred for 
the variable, enum or function return value that is being 
assigned. Something like:

```D
void fun(string);

T enum(T) enumT = /+...+/;
T funT(T)(T[]) => /+...+/;

fun(enumT); // fun(enumT!string)
fun(funT([])); // fun(funT(cast(string[]) []))
```

This would, as I understand it, mean that `std.conv.to` could be 
called without the template parameter when it is assigned to 
something with an unambiguous type: `int x = 13.4.to;`.





More information about the Digitalmars-d mailing list