Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.80 KB | None | 0 0
  1.     // get the Map module
  2.     let map = mapType.Assembly.GetTypes() |> Array.find(fun t -> t.Name = "MapModule")
  3.     // get the MapModule.iter function
  4.     let iter = map.GetMethods(BindingFlags.Static ||| BindingFlags.Public) |> Array.find (fun m -> m.Name = "Iterate")
  5.     // create a unction that we want to pass to Map.iter
  6.     let iterator = (fun k v -> printfn k)
  7.     // get type of iterator function
  8.     let fType = iterator.GetType()
  9.     // tried to build a function type
  10.     let f1Type = FSharpType.MakeFunctionType ( fType, FSharpType.MakeFunctionType(ttt4.GetType(), typeof<unit>  ) )
  11.     // specialise the generic function
  12.     let genericIter = iter.MakeGenericMethod([| fType  ; ttt4.GetType() |])
  13.  
  14.     //invoke the Map.iter function with our iterator function, and the map instance ttt4
  15.     genericIter.Invoke(null,  [| iterator; ttt4 |]) |> ignore
  16.  
  17.  
  18.  
  19. (*
  20.  
  21. Unhandled Exception: System.ArgumentException: Object of type 'Program+iterator@288TT[System.Object,System.Object]' cannot be converted to type
  22.  
  23. 'FSharpFunc`2[
  24.          Program+iterator@288TT[System.Object,System.Object],
  25.          FSharpFunc`2[
  26.             FSharpMap`2[
  27.               System.String,
  28.               User+User
  29.             ],
  30.             Unit
  31.           ]
  32. ]'.
  33.  
  34.  
  35.    at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
  36.    at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
  37.    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  38.    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  39.  
  40. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement