Skip to content

Instantly share code, notes, and snippets.

@alfonsogarciacaro
Created February 16, 2017 13:54
Show Gist options
  • Save alfonsogarciacaro/27335d9fbd43b8d1526a086bf0311964 to your computer and use it in GitHub Desktop.
Save alfonsogarciacaro/27335d9fbd43b8d1526a086bf0311964 to your computer and use it in GitHub Desktop.
Implicit conversion for erased unions
type U2<'a,'b> =
| Case1 of 'a
| Case2 of 'b
static member op_Implicit(x:'a) = U2.Case1 x
static member op_Implicit(x:'b) = U2.Case2 x
let inline (!|) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
let foo (arg: U2<string, int>) = ()
foo(!|"hello")
foo(!|5)
//foo(!|5.) // Doesn't compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment