September 07, 2016
On Tuesday, 6 September 2016 at 14:21:26 UTC, Steven Schveighoffer wrote:
> On 9/5/16 5:59 AM, 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
>
>       auto test3 = simple!int();
>
> Granted, you are still typing "auto", but only specify the type once.
>
> -Steve

Only for the simple case.

It would interesting if it would work for:

struct Blah
{
   string name;
}

Blah b;
b.name = simple();


or:

void myFunc(string s) { ... }
myFunc(simple());

Andrea




September 08, 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");




September 08, 2016
On 9/7/16 3:19 AM, Andrea Fontana wrote:
> On Tuesday, 6 September 2016 at 14:21:26 UTC, Steven Schveighoffer wrote:
>> On 9/5/16 5:59 AM, 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
>>
>>       auto test3 = simple!int();
>>
>> Granted, you are still typing "auto", but only specify the type once.
>>
>> -Steve
>
> Only for the simple case.
>
> It would interesting if it would work for:
>
> struct Blah
> {
>    string name;
> }
>
> Blah b;
> b.name = simple();
>
>
> or:
>
> void myFunc(string s) { ... }
> myFunc(simple());

Right, but there is a problem here. The connection of the simple return type to the simple template parameter is not necessarily guaranteed. That is, there is no guarantee simple!int is going to return int (for any template).

This means the compiler has to determine how to make simple return an int, and this isn't always obvious, or easy to determine.

You can, of course, rewrite simple to take advantage of IFTI:

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

simple(b.name);

I'd call it initialize, and do it this way:

b.name.initialize();

-Steve
September 08, 2016
On Thursday, 8 September 2016 at 13:28:15 UTC, Steven Schveighoffer wrote:
> Right, but there is a problem here. The connection of the simple return type to the simple template parameter is not necessarily guaranteed. That is, there is no guarantee simple!int is going to return int (for any template).
>
> This means the compiler has to determine how to make simple return an int, and this isn't always obvious, or easy to determine.

I don't know how compiler works, but my logic was:

- I have T simple(T) { ... }
- return type == template argument
- int ... = simple() => T == int => simple!int

I undestand that if it was:

auto simple(T) { ... } it should be difficult

Probably I'm missing someting about compiler internal structure, not my field.

Andrea


1 2
Next ›   Last »