Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. open System.Runtime.InteropServices
  2.  
  3. type C0() = static member M([<Out>] __: byref<C0>) = false // byref<C0> -> bool
  4. type C1() = static member M([<Out>] arg: byref<C1>) = arg <- C1(); true // byref<C1> -> bool
  5. type C2() = static member M(arg: byref<C2>) = arg <- C2(); true // byref<C2> -> bool
  6. type C3() = static member M(arg: outref<C3>) = arg <- C3(); true // outref<C3> -> bool
  7.  
  8. C0.M // val it : unit -> bool * C0
  9. C1.M // val it : unit -> bool * C1
  10. C2.M // val it : C2 ref -> bool
  11. C3.M // val it : unit -> bool * C2
  12.  
  13. let inline getByRef<'a when 'a: (static member M: byref<'a> -> bool)> () =
  14. let mutable res = Unchecked.defaultof<_>
  15. if (^a: (static member M: ^a byref -> bool) &res)
  16. then Some res
  17. else None
  18.  
  19. let inline getOutRef<'a when 'a: (static member M: outref<'a> -> bool)> () =
  20. let mutable res = Unchecked.defaultof<_>
  21. if (^a: (static member M: outref<'a> -> bool) &res)
  22. then Some res
  23. else None
  24.  
  25. getByRef<C0>() //works
  26. getByRef<C1>() //works
  27. getByRef<C2>() //works
  28. getByRef<C3>() //runtime error FS0193: internal error: Cannot get TypeToken for a ByRef type.
  29.  
  30. getOutRef<C0>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
  31. getOutRef<C1>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
  32. getOutRef<C2>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
  33. getOutRef<C3>() //runtime error FS0193: internal error: Cannot get TypeToken for a ByRef type.
  34.  
  35.  
  36. //works
  37. match C0.M() with
  38. | true, value -> Some value
  39. | false, _ -> None
  40.  
  41. //works
  42. match C1.M() with
  43. | true, value -> Some value
  44. | false, _ -> None
  45.  
  46. //compile error FS0001: This expression was expected to have type 'C2 ref' but here has type 'unit'
  47. match C2.M() with
  48. | true, value -> Some value
  49. | false, _ -> None
  50.  
  51. //works
  52. match C3.M() with
  53. | true, value -> Some value
  54. | false, _ -> None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement