Re: [Vala] Newbie help needed for Vala delegates/lambda expressions



From: Manish Jain <bourne identity hotmail com>
Sent: Wednesday, August 30, 2017, 10:31:00 AM GMT+1
Subject: [Vala] Newbie help needed for Vala delegates/lambda expressions

After programming for a long time with C/C++, I am trying to get 
comfortable with Vala for my GUI programming.

Hi, and welcome to Vala land.

Here are 2 delegates I set up to add a couple of integers :
delegate int _dg_add2(int a, int b);

_dg_add2 f_add2(int a, int b)
{
     return () => a + b;
}

int c2 = f_add2(a, b);  //Problem here

The compiler gives me the message : error: Assignment: Cannot convert 
from `_dg_add2' to `int'. But then, a similar conversion was reached 
successfully with the first call to f_add

So what is wrong with my understanding ? Thanks for any help.

You are returning a function, not an integer. That is what the error messageis telling you. To evaluate the 
function, which does return an integer, you needto place parentheses after the function. So:
int c2 = f_add2(a, b);  //Problem here
should be:
int c2 = f_add2(a, b)();  //Problem almost gone
Your type signature (the delegate) is also wrong for the return function.So: 
delegate int _dg_add2(int a, int b);
should be:
delegate int _dg_add2();

That's because your lambda expression doesn't have any parameters.
Regards,

Al


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]