Re: function type parameter inference not working

2017-04-23 Thread ag0aep6g via Digitalmars-d-learn

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 */
}



Re: function type parameter inference not working

2017-04-23 Thread XavierAP via Digitalmars-d-learn

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.


Re: function type parameter inference not working

2017-04-25 Thread XavierAP via Digitalmars-d-learn

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?


Re: function type parameter inference not working

2017-04-25 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: function type parameter inference not working

2017-04-25 Thread XavierAP via Digitalmars-d-learn

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.


Re: function type parameter inference not working

2017-04-25 Thread Ali Çehreli via Digitalmars-d-learn

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