Skip to content

Anonymous delegates

Nick Schrader requested to merge nschrader/vala:wip/issue/658 into master

Final implementation for issue #658.

This PR implements anonymous delegates to be exclusively used as parameter types. They are a simplified expression for named delegates, but do not support all their language features. Inner parameters are anonymous (without identifier) to be even more short-hand.

void f (delegate(int, int) => int d) {
   stdout.printf ("%d\n", d(1, 2));
}

void main () {
   f ((a, b) => a + b);
}

Restrictions are:

  • no throws-clause, named delegates should be preferred in this case
  • anonymous delegates cannot return an anonymous delegate, named delegates should be preferred in this case
  • outer dynamic and params modifiers are not allowed, as they would require arrays of anonymous delegates and run-time information that is unavailable to this implementation
  • outer ref and out modifiers are not permitted as they would require the anonymous type to be reused elsewhere
  • inner ref, out, unowned and weak modifiers are allowed
  • no inner dynamic and params modifiers, named delegates should be preferred in this case
  • inner optional parameters are not permitted as the argument would need to be named and not anonymous to retrieve the default value
  • inner and outer code attributes are accepted, but a warning is thrown if they are ambiguous (void f([CCode (scope = "async")] delegate() => void d) could apply to the parameter d or the anonymous delegate; in doubt it is always applied to the parameter)

Moreover, the following functionalities come along:

  • warning saying that anonymous delegates are still experimental
  • meaningful error messages
  • pretty printing of anonymous delegates in compiler messages
  • updated (and tested) CodeWriter
  • 20 tests
Edited by Nick Schrader

Merge request reports