Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 05, 2016 Return type deduction | ||||
---|---|---|---|---|
| ||||
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 |
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | Dne 5.9.2016 v 11:59 Andrea Fontana via Digitalmars-d napsal(a):
> 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.
> ...
> Andrea
AFAIK this is impossible
|
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On 05.09.2016 12:25, Daniel Kozak via Digitalmars-d wrote:
> Dne 5.9.2016 v 11:59 Andrea Fontana via Digitalmars-d napsal(a):
>
>> 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.
>> ...
>> Andrea
>
> AFAIK this is impossible
>
It's a feature request. (Lambdas also do type deduction based on expected type, now just do the same during IFTI, as a fallback in case not enough information can be deduced from the arguments alone.)
|
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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.
|
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 :)
Return type deduction probably won't work, but implicit casts of a custom data type could do this - that's how C++ would do it.
Alas, D does not have an implicit cast operator :( best we have is `alias this` but it only allows one right now (despite the book saying it is supposed to do multiple.. and there are some PRs about it). They wouldn't be templated but json could just do a few implicit casts.
|
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lodovico Giaretta | On Monday, 5 September 2016 at 11:51:23 UTC, Lodovico Giaretta wrote:
> 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.
I think it should be get!int(). It seems logical and it doesn't break existing code.
|
September 06, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 05.09.2016 14:31, Adam D. Ruppe wrote: > 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 :) > > Return type deduction probably won't work, Why? It seems rather easy to do. > but implicit casts of a > custom data type could do this - that's how C++ would do it. > ... That does not solve the problem that nicely: if no different type is specified the dummy object with the opImplicitCast can be assigned to a variable: auto x = a.get(); // oops This shouldn't compile. > Alas, D does not have an implicit cast operator :( best we have is > `alias this` but it only allows one right now (despite the book saying > it is supposed to do multiple.. and there are some PRs about it). They > wouldn't be templated but json could just do a few implicit casts. |
September 05, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On 9/5/2016 2:59 AM, Andrea Fontana wrote:
> Is there any chance to implement this kind of deduction?
No. Types are resolved bottom-up, not top-down. Trying to do both may not even be doable in the general case, it certainly makes for all kinds of complexity. It makes a soup out of overload resolution, for example.
|
September 06, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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
|
September 07, 2016 Re: Return type deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana Attachments:
| On Mon, Sep 5, 2016 at 11:59 AM, Andrea Fontana via Digitalmars-d < digitalmars-d@puremagic.com> 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
>
user.name = json.get!(info.name);
or
user.name = json!(info.name);
possible right now. No language changes. Shorter. No strings. compile time type verification.
limitation:
can't do runtime loading of unknown json structure (but then neither does
your proposal).
R
|
Copyright © 1999-2021 by the D Language Foundation