Idea: Reverse Type Inference

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed May 24 06:03:37 PDT 2017


On 5/23/17 5:20 AM, Ola Fosheim Grøstad wrote:
> 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.

This is different. It's IFTI based on return type.

BTW, swift does this too, and I have to say it's useful in a lot of cases.

For example, any time you have a variant-like interface, and an already 
existing type, something like this looks so much cleaner.

struct Point
{
   int x;
   int y;
}

auto row = getRowFromDB();
auto p = Point(row.get!int("x"), row.get!int("y")); // currently required
auto p = Point(row.get!(typeof(Point.x))("x"), 
row.get!(typeof(Point.y))("y")); // more generic, but horribly ugly.
auto p = Point(row.get("x"), row.get("y")); // with better IFTI

-Steve


More information about the Digitalmars-d mailing list