Created
May 13, 2023 08:30
-
-
Save MelbourneDeveloper/a7f4905796951ee9d7d36b5f949e932d to your computer and use it in GitHub Desktop.
Bloc Style With Sealed Event Types
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
//Programming is too easy. What if we wrote functions like this? | |
//Instead of just calling the function, we create types to bundle | |
//up the arguments and then pass the type to the function that accepts | |
//a generic type. | |
// | |
//This is the essence of the Bloc pattern. | |
sealed class Event {} | |
class AdditionArguments extends Event { | |
final num a; | |
final num b; | |
AdditionArguments(this.a, this.b); | |
} | |
num calculateBlocStyle<T extends Event>(T event) => switch (event) { | |
(AdditionArguments e) => e.a + e.b, | |
}; | |
/// Or, we could just do this... | |
num calculate(num a, num b) => a + b; | |
//And this doesn't even take into account attaching | |
//and detaching streams... | |
//Obviously Bloc style is way more verbose, makes the code harder to read, | |
//introduces extra code paths that you need to handle, which makes it | |
//more error prone, is generally clunky, and basically defies the whole | |
//point of functions. But hey, it unifies our code right? | |
void main() { | |
print('Hello world: ${calculateBlocStyle(AdditionArguments(1, 1))}!'); | |
print('Hello world: ${calculate(1, 1)}!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment