Advertisement
AngraMainyu

Untitled

Jul 17th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.93 KB | None | 0 0
  1. // our interface
  2. type IFoo = interface end
  3. // concrete type
  4. type Foo = inherit IFoo
  5. // interface with a generic argument
  6. type IBar<'foo when 'foo :> IFoo> = interface
  7.     // we want to call BazUnion.Baz on an instance of IBar<Foo>, but there's no way to get the Foo out of the IBar
  8.     // so we make that magic happen here.
  9.     // in the concrete implementation of IBar, you'd keep a reference to an IFoo somewhere and then implement this
  10.     // to dereference it
  11.     abstract GetFoo : unit -> 'foo
  12.    end
  13.  
  14. // union with one type representing our interface
  15. type BazUnion = Baz of IFoo // should be B of #A, but how?
  16.  
  17. // the generic version, if you like
  18. // flexible types are used to constrain generic type arguments to a particular base type
  19. //type GenericBaz<'foo when 'foo :> #IFoo> = Baz of 'foo
  20.  
  21. module test =
  22.     //val useSpecific: IBar<Foo> -> BazUnion (Baz of IFoo)
  23.     let useSpecific(bar: IBar<Foo>) = Baz(bar.GetFoo())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement