Created
December 6, 2022 16:53
-
-
Save AngelMunoz/00a418e0cf44ee4c46da14cc8eb4f886 to your computer and use it in GitHub Desktop.
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
// differentiate the kind of payload you want to take | |
type AdditionPayload = | |
| Integers of int * int | |
| Strings of string * string | |
module Operations = | |
// handle your integers | |
let private addInts a b = a + b | |
// handle your strings | |
let private addStrings (a: string) (b: string) = (int a) + (int b) | |
// an operation that takes a union and based on the payload | |
// provides the correct operation | |
let sum (values: AdditionPayload) = | |
match values with | |
| Integers (a, b) -> addInts a b | |
| Strings (a, b) -> addStrings a b | |
Operations.sum (Integers (1, 2)) | |
Operations.sum (Strings ("10", "20")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment