local functions to delegates
A local function can not be converted to a static delegate. e.g This code doesn't compile:
[CCode (has_target = false)]
delegate int TestDelegateFunction ();
int TestDelegate (TestDelegateFunction f)
{
return f ();
}
void main()
{
int LocalFunction ()
{
return 1;
}
stdout.printf ("%i\n",TestDelegate (LocalFunction));
}
vala compiler gives:
code.vala:11.5-11.24: warning: local functions are experimental
11 | int LocalFunction ()
| ^~~~~~~~~~~~~~~~~~~~
code.vala:15.67-15.79: error: Argument 1: Cannot convert from `delegate int _LocalFunc0_ ()' to `delegate
int TestDelegateFunction ()'
15 | stdout.printf ("%i\n",TestDelegate (LocalFunction));
| ^~~~~~~~~~~~~
Compilation failed: 1 error(s), 1 warning(s)
It would be nice if this code worked (as the local function isn't using out of scope variables) or as an alternative, that we could create local static functions e.g.
[CCode (has_target = false)]
delegate int TestDelegateFunction ();
int TestDelegate (TestDelegateFunction f)
{
return f ();
}
void main()
{
[CCode (has_target = false)]
int LocalFunction ()
{
return 1;
}
stdout.printf ("%i\n",TestDelegate (LocalFunction));
}
Edited by Ben Corby