Idea: Reverse Type Inference

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Tue May 23 02:20:21 PDT 2017


On Monday, 22 May 2017 at 10:13:02 UTC, Sebastiaan Koppe wrote:
> Over the past weeks I have been noticing a specific case where 
> it happens. I call it reverse type inference, simply because it 
> goes against the normal evaluation order.

I think what you want, in the general sense, is basically 
overloading based on return-type. Which I think is a very useful 
and cool feature.

But D moste likely follows  C++ by doing resolution bottom-up 
(starting from the leaves of the AST).

For the requested feature you need to do resolution top-down 
(which I believe Ada does).

The downside of this is when you hit ambiguities, thanks to 
implicit conversion/coercion (which I think is mostly a 
mal-feature and a source for bugs).

One solution to this is to give preference to the match that use 
the fewest implicit conversions.

But I think the better solution is to:

1. remove implicit type conversion

2. add a more compact notation for indicating desired type so 
that you can call the desired function without using the return 
value. (i.e. "f(x):int" to request the version of f(x) that 
returns int)

You might hack together a solution just for templates, but it 
makes the language even less orthogonal than it is. So I think D 
is better off doing it in a manner that is consistent and avoids 
more special cases.



More information about the Digitalmars-d mailing list