Return type deduction

Patric via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 8 04:45:09 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 thought that i have see your idea in another lang, but maybe 
i´m wrong)

You can use ref, if you want to simplify your code;

void simple(T)( ref T t) { t = T.init; }

so maybe your example of the json can be something like this:

json.set(user.name,"info/name");

Or with some more little tricks:

alias json_set = json.set;
user.name.json_set("info/name");






More information about the Digitalmars-d mailing list