TapFunction<A> typedef
The tap function takes a side effect function Function that is applied to the value inside the Some instance of Option. It returns the original Option after performing the side effect on the value. If the input Option is None, the function simply returns the same None.
This function allows you to perform side effects like printing or logging the value inside Some without changing the value itself.
The tap function is commonly used in functional programming to observe values in a container without modifying them.
Example:
void main() {
Option<String> someOption = Some("Hello");
Option<String> noneOption = const None();
// Printing the value inside Some without modifying the value
tap<String>((value) => print("Value inside Some: $value"))(someOption); // Output: Value inside Some: Hello
// Since it's None, nothing will be printed
tap<String>((value) => print("Value inside None: $value"))(noneOption); // No output
}
Implementation
typedef TapFunction<A> = Option<A> Function(Option<A> option);