Created
December 15, 2023 20:10
-
-
Save v1993/0a7e49b9ffddc91a678a038a0860ac52 to your computer and use it in GitHub Desktop.
Messing with delegates in vala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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