Skip to content

Instantly share code, notes, and snippets.

@SchlenkR
Created March 10, 2019 11:12
Show Gist options
  • Save SchlenkR/78d3bf0d2f3deaf2a066b43ba90d369e to your computer and use it in GitHub Desktop.
Save SchlenkR/78d3bf0d2f3deaf2a066b43ba90d369e to your computer and use it in GitHub Desktop.
// extend a type from another assembly with 'combine'
type List<'a> with
static member combine (f, x: List<'a>) : List<'a> = x @ [f]
// extend another type from another assembly with 'combine'
type System.Int32 with
static member combine (f, x: System.Int32) = x + f
// extend a type from this assembly with 'combine'
type GenTest<'a> = { value: 'a } with
static member combine (f, x: GenTest<_>) = { value = f }
// the generic 'combine' function
let inline combine f (x: ^t) =
(^t: (static member combine: _ * ^t -> ^t) (f,x))
///// usage:
// this is working:
List<_>.combine (2, [1;2;3])
System.Int32.combine (5, 10)
// not working ("The type '...' does not support the operator 'combine'"):
[1;2;3] |> combine 1
5 |> combine 10
// these are of course working:
{ value = 10 } |> combine 5
{ value = "yyy" } |> combine "zzz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment