digitalmars.D.learn - The type inference everywhere
- Salih Dincer (12/12) Oct 31 2021 ```d
- =?UTF-8?Q?Ali_=c3=87ehreli?= (12/21) Oct 31 2021 Makes sense because e.g. the following works:
- data pulverizer (20/42) Oct 31 2021 This is a teachable moment for me, so I'll ask the question. Why
- user1234 (4/45) Oct 31 2021 To me it is the right answer. Maybe that OP wanted the
- data pulverizer (2/5) Oct 31 2021 Ah, I see. Interesting proposal.
- Salih Dincer (3/25) Oct 31 2021 Actually, foo is inside a class. So using T is very easy.
```d auto foo(int value, auto s = Section(2, 60)) { int max; /* ^------- ? ... */ return Section (0, max) } ``` Is possible something like above pointed. OK, I know it isn't because I tried! Well, wouldn't it be nice if it did? Why shouldn't the inference be possible on default argument? Peek out of rvalue... Thanks...
Oct 31 2021
On 10/31/21 7:07 AM, Salih Dincer wrote:```d auto foo(int value, auto s = Section(2, 60)) { int max; /* ^------- ? ... */ return Section (0, max) } ``` Is possible something like above pointed. OK, I know it isn't because I tried! Well, wouldn't it be nice if it did?Makes sense because e.g. the following works: struct S { auto i = 42; } I bet the problem with your proposal is "auto" in that position is a part of the two-word "auto ref" parameters. I don't know how hard or impossible it would be to allow just "auto" there. In any case, although it would be nice for completeness, the use case is so rare that I wouldn't mind repeating the type twice in that usage. But I agree with you... Ali
Oct 31 2021
On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote:On 10/31/21 7:07 AM, Salih Dincer wrote:This is a teachable moment for me, so I'll ask the question. Why isn't something like this the answer? ``` import std.stdio: writeln; struct Section { int x; int y; } auto foo(T)(int value, auto ref T s = Section(2, 60)) { //... return Section(0, 60); } void main() { foo(5).writeln; } ``````d auto foo(int value, auto s = Section(2, 60)) { int max; /* ^------- ? ... */ return Section (0, max) } ``` Is possible something like above pointed. OK, I know it isn'tbecause Itried! Well, wouldn't it be nice if it did?Makes sense because e.g. the following works: struct S { auto i = 42; } I bet the problem with your proposal is "auto" in that position is a part of the two-word "auto ref" parameters. I don't know how hard or impossible it would be to allow just "auto" there. In any case, although it would be nice for completeness, the use case is so rare that I wouldn't mind repeating the type twice in that usage. But I agree with you... Ali
Oct 31 2021
On Sunday, 31 October 2021 at 17:51:45 UTC, data pulverizer wrote:On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote:To me it is the right answer. Maybe that OP wanted the TemplateType parameter to be implicitly added (and that's why Ali interpreted the question as a language proposal)?On 10/31/21 7:07 AM, Salih Dincer wrote:This is a teachable moment for me, so I'll ask the question. Why isn't something like this the answer? ``` import std.stdio: writeln; struct Section { int x; int y; } auto foo(T)(int value, auto ref T s = Section(2, 60)) { //... return Section(0, 60); } void main() { foo(5).writeln; } ```[...]because I[...]Makes sense because e.g. the following works: struct S { auto i = 42; } I bet the problem with your proposal is "auto" in that position is a part of the two-word "auto ref" parameters. I don't know how hard or impossible it would be to allow just "auto" there. In any case, although it would be nice for completeness, the use case is so rare that I wouldn't mind repeating the type twice in that usage. But I agree with you... Ali
Oct 31 2021
On Sunday, 31 October 2021 at 18:51:09 UTC, user1234 wrote:To me it is the right answer. Maybe that OP wanted the TemplateType parameter to be implicitly added (and that's why Ali interpreted the question as a language proposal)?Ah, I see. Interesting proposal.
Oct 31 2021
On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote:On 10/31/21 7:07 AM, Salih Dincer wrote:Actually, foo is inside a class. So using T is very easy. Moreover,it's possible to do it with ```alias S = Section;``````d auto foo(int value, auto s = Section(2, 60)) { int max; /* ^------- ? ... */ return Section (0, max) } ``` Is possible something like above pointed. OK, I know it isn'tbecause Itried! Well, wouldn't it be nice if it did?Makes sense because e.g. the following works: struct S { auto i = 42; } I bet the problem with your proposal is "auto" in that position is a part of the two-word "auto ref" parameters. I don't know how hard or impossible it would be to allow just "auto" there. In any case, although it would be nice for completeness, the use case is so rare that I wouldn't mind repeating the type twice in that usage. But I agree with you... Ali
Oct 31 2021