[Issue 10491] Type inference for function arguments with default value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jun 29 01:24:27 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10491



--- Comment #3 from bearophile_hugs at eml.cc 2013-06-29 01:24:25 PDT ---
(In reply to comment #2)

Thank you for the comments Kenji.

> 1. Default argument is not so often used. Then, reduction amount of the code is
> very limited.

I agree. This is why I said it's not an important enhancement request. It's
handy, but not often.


> 2. It breaks consistency of C-style languages that all of function parameters
> in function signature have their own type explicitly.

I think this is not much relevant. It doesn't break valid C code.


> 3. It would introduce one more forward reference possibility.
> 
> void foo(auto a = bar()) {}   // inference
> 
> auto bar()
> {
>     alias T = ParameterTypeTuple!foo[0];   // requires foo's complete signature
>     return T.init;
> }
> 
> void main()
> {
>     auto x = bar();
> }
> 
> Already, auto return, function attribute inference has same possibility. They
> sometimes produces legitimate (but difficult to understand) forward reference
> errors.

Right, the more type inference you add, the more complex is for the compiler
(and sometimes for the programmer too) to understand the code and make it work.
Using ParameterTypeTuple on an inferenced argument type looks like a way to
stress the type inferencer.


> 4. `auto ref` / `in ref` is still debatable. This enhancement might have
> interference with it.

"in ref" is not a problem, because this is not valid code:

struct Foo { int x; }
int foo(in ref Foo f = Foo(1)) {}

So this is not valid code:

struct Foo { int x; }
int foo(in ref f = Foo(1)) {}


This is currently valid:

struct Foo { int x; }
int foo()(auto ref Foo f = Foo(1)) {}

So this should be valid:

struct Foo { int x; }
int foo()(auto ref f = Foo(1)) {}



> Side note: Today, issue 7152 is implemented. By that,
> 
> void foo(in VeryLongNamedStruct x = VeryLongNamedStruct(1)) {}
> 
> might be shorten to:
> 
> void foo(in VeryLongNamedStruct x = 1) {}

Right. But my use case was a bit more complex, more like:

void foo(in VeryLongNamedStruct y = VeryLongNamedStruct(1, 2)) {}


I am grateful to Henning Pohl for writing the patch.
So do we close down this enhancement request?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list