Skip to content

Instantly share code, notes, and snippets.

@raveesh-me
Created October 7, 2024 16:45
Show Gist options
  • Save raveesh-me/fe0a210324fb65131b458d86473838ac to your computer and use it in GitHub Desktop.
Save raveesh-me/fe0a210324fb65131b458d86473838ac to your computer and use it in GitHub Desktop.
RBAC / Extension Guard for functions that take in an action
// Extension functions
// Generic Programming
extension XFunctionGuard<T extends Function> on T {
T callWithGuard(String action) {
if (action != "callable") {
throw Exception();
}
return this.call;
}
}
void main() async {
Function hi = () => print("hi");
Future<String> lateHi() async {
await Future.delayed(Duration(seconds: 1), (){});
return "hi, Late!";
}
hi.callWithGuard("callable")();
print(await lateHi.callWithGuard("callable2")());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment