Skip to content

Instantly share code, notes, and snippets.

@TheAngryByrd
Created October 11, 2022 23:08
Show Gist options
  • Save TheAngryByrd/8a020be6a00d1488e3cb8886e9215796 to your computer and use it in GitHub Desktop.
Save TheAngryByrd/8a020be6a00d1488e3cb8886e9215796 to your computer and use it in GitHub Desktop.
SRTP-Active-Patterns.fs
module NameOps =
let inline (|HasName|) x = (^a : (member Name : string) x)
let inline (|HasAge|) x = (^a : (member Age : int) x)
let inline (|IsIdentifyable|) (HasName name & HasAge age) = name, age
let inline doWithIsIdentifyable (IsIdentifyable (name,age)) =
printfn $"{name} - {age}"
type Dog = {
Name : string
Age: int
FavoriteChewToy : string
}
type Cat = {
Name : string
Age : int
FeedingTime : System.DateTime
}
let dog = {
Name = "Fido"
Age = 3
FavoriteChewToy = "My shoes"
}
let cat = {
Name = "Mr Muffin"
Age = 30
FeedingTime = System.DateTime.Now
}
NameOps.doWithIsIdentifyable dog
NameOps.doWithIsIdentifyable cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment