Thread overview
function type parameter inference not working
Apr 23, 2017
XavierAP
Apr 23, 2017
ag0aep6g
Apr 23, 2017
XavierAP
Apr 25, 2017
XavierAP
Apr 25, 2017
Ali Çehreli
Apr 25, 2017
XavierAP
Apr 25, 2017
Ali Çehreli
April 23, 2017
It's not working for my case, while I see no special reason why it couldn't. Also I can't find specific inference rules at http://dlang.org/spec/function.html#function-templates

Is it a problem that the types to be inferred are in turn also templated? Any workaround that can make inference work? Otherwise I would re-consider my design rather than having to specify types already available in the runtime arguments :(

void assembleMass1D(Mat, Vec)(ref Mat M, const ref Vec x)
{ /* ... */ }
Matrix!(2,2) M = /* ... */;
Vector!2     V = /* ... */;
assembleMass1D(M, V); // ERROR template cannot deduce function from argument types
April 23, 2017
On 04/23/2017 09:33 PM, XavierAP wrote:
> void assembleMass1D(Mat, Vec)(ref Mat M, const ref Vec x)
> { /* ... */ }
> Matrix!(2,2) M = /* ... */;
> Vector!2     V = /* ... */;
> assembleMass1D(M, V); // ERROR template cannot deduce function from
> argument types

Please post self-contained code. When I fill the gaps you left, it works for me:

----
struct Matrix(uint a, uint b) {}
struct Vector(uint a) {}

void assembleMass1D(Mat, Vec)(ref Mat M, const ref Vec x)
{ /* ... */ }

void main()
{
    Matrix!(2,2) M;
    Vector!2     V;
    assembleMass1D(M, V); /* no error */
}
----
April 23, 2017
On Sunday, 23 April 2017 at 19:40:39 UTC, ag0aep6g wrote:
>
> Please post self-contained code. When I fill the gaps you left, it works for me:

Interesting, thanks a lot. I'll test and narrow down what's in my code preventing this from working (I can't really think of anything) and I'll report back.
April 25, 2017
On Sunday, 23 April 2017 at 19:40:39 UTC, ag0aep6g wrote:
>
> Please post self-contained code. When I fill the gaps you left, it works for me:

Found it! It stops working (DMD v2.073.0 for Windows) if it has to infer the type of a temporary local variable -- constructed in place of the argument:


struct Matrix(size_t nr, size_t nc) {}
struct Vector(size_t n) {}

void assembleMass1D(Mat, Vec)(ref Mat M, const ref Vec x)
{ /* ... */ }

Matrix!(2,2) M;
Vector!2     V;
assembleMass1D(M, V);          // OK
assembleMass1D(M, Vector!2()); // ERROR template cannot deduce function


Is this a bug?
April 25, 2017
On 04/25/2017 12:19 PM, XavierAP wrote:

> void assembleMass1D(Mat, Vec)(ref Mat M, const ref Vec x)
> { /* ... */ }
>
> Matrix!(2,2) M;
> Vector!2     V;
> assembleMass1D(M, V);          // OK
> assembleMass1D(M, Vector!2()); // ERROR template cannot deduce function
>
>
> Is this a bug?

This is an intentional limitation of D. It's not possible to bind rvalues (temporaries) to reference parameters. The best option here is 'auto ref':

void assembleMass1D(Mat, Vec)(auto ref Mat M, auto ref const(Vec) x)
{ /* ... */ }

Ali

April 25, 2017
On Tuesday, 25 April 2017 at 19:57:30 UTC, Ali Çehreli wrote:
>
> This is an intentional limitation of D. It's not possible to bind rvalues (temporaries) to reference parameters. The best option here is 'auto ref':

Aha I had forgotten about the ref (obviously, otherwise I wouldn't have passed a temporary even in the unit test -- I'm embarrassed). If that's the reason why it doesn't work I'm satisfied.

It would be helpful if the error message talked about the ref qualifier as the root cause instead of the consequent failure to infer template parameters.
April 25, 2017
On 04/25/2017 01:02 PM, XavierAP wrote:

> It would be helpful if the error message talked about the ref qualifier
> as the root cause instead of the consequent failure to infer template
> parameters.

That's a common request but the solution is not implemented yet because it's not trivial if you consider multiple overloads.

The compiler must remember which parameters failed and present it in a readable format: "I considered these overloads and this failed for parameter 1 and this other one failed for parameter 2 (although it might succeed if it were not ref), etc."

Ali