Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Created December 6, 2022 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngelMunoz/00a418e0cf44ee4c46da14cc8eb4f886 to your computer and use it in GitHub Desktop.
Save AngelMunoz/00a418e0cf44ee4c46da14cc8eb4f886 to your computer and use it in GitHub Desktop.
// 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