Skip to content

Instantly share code, notes, and snippets.

@v1993
Created December 15, 2023 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v1993/0a7e49b9ffddc91a678a038a0860ac52 to your computer and use it in GitHub Desktop.
Save v1993/0a7e49b9ffddc91a678a038a0860ac52 to your computer and use it in GitHub Desktop.
Messing with delegates in vala
delegate int MyFunc ();
class MyNotifier {
public MyNotifier () {
print ("Created\n");
}
~MyNotifier () {
print ("Destroyed\n");
}
public void method () {
print ("Hi\n");
}
}
void make_some_functions (int initializer, out MyFunc incrementor, out MyFunc decrementor, out MyFunc getter) {
// Uncomment to make vala split `notifier` into a separate block and thus free it earlier
//{
var notifier = new MyNotifier ();
incrementor = () => {
notifier.method ();
return initializer++;
};
//}
decrementor = () => {
return initializer--;
};
getter = () => {
return initializer;
};
}
void main () {
MyFunc incrementor, decrementor, getter;
make_some_functions(5, out incrementor, out decrementor, out getter);
print ("%i\n", incrementor());
incrementor = null;
print ("%i\n", decrementor());
print ("%i\n", getter());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment