Return type deduction
Lodovico Giaretta via Digitalmars-d
digitalmars-d at puremagic.com
Mon Sep 5 04:51:23 PDT 2016
On Monday, 5 September 2016 at 09:59:16 UTC, Andrea Fontana wrote:
> I asked this some time (years?) ago. Time for a second try :)
>
> Consider this:
>
> ---
>
> T simple(T)() { return T.init; }
>
>
> void main()
> {
> int test = simple!int(); // it compiles
> int test2 = simple(); // it doesn't
> }
>
> ---
>
> Is there any chance to implement this kind of deduction?
> Please notice that it doesn't break any existing code, I guess.
>
> For example using my json wrapper [1] this sounds a bit
> pedantic:
>
> ----
> user.name = json.get!string("info/name");
> user.age = json.get!int("info/age");
> ----
>
> If return type deduction could be implemented it would be:
>
> ----
> user.name = json.get("info/name");
> user.age = json.get("info/age");
> ----
>
> [1] https://code.dlang.org/packages/jsonwrap
>
> Andrea
I see just one problem in this request:
===========================
T get(T = int)()
{
return T.init;
}
double val = get(); // should this call get!double or try to call
get!int and give an error?
===========================
That is, what takes precedence? Default argument or inference
from the destination type? I'd personally prefer this to give an
error, as it is ambiguous and error prone.
More information about the Digitalmars-d
mailing list