Skip to content

Instantly share code, notes, and snippets.

@palladin
Created January 22, 2014 16:22
Show Gist options
  • Save palladin/8561783 to your computer and use it in GitHub Desktop.
Save palladin/8561783 to your computer and use it in GitHub Desktop.
My Favorite Active Pattern
let (|Named|Array|Ptr|Param|) (t : System.Type) =
if t.IsGenericType
then Named(t.GetGenericTypeDefinition(), t.GetGenericArguments())
elif t.IsGenericParameter
then Param(t.GenericParameterPosition)
elif not t.HasElementType
then Named(t, [| |])
elif t.IsArray
then Array(t.GetElementType(), t.GetArrayRank())
elif t.IsByRef
then Ptr(true, t.GetElementType())
elif t.IsPointer
then Ptr(false, t.GetElementType())
else failwith "MSDN says this can’t happen"
@palladin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment