Created
October 7, 2024 16:45
-
-
Save raveesh-me/fe0a210324fb65131b458d86473838ac to your computer and use it in GitHub Desktop.
RBAC / Extension Guard for functions that take in an action
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
// 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