Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Runtime.InteropServices
- type C0() = static member M([<Out>] __: byref<C0>) = false // byref<C0> -> bool
- type C1() = static member M([<Out>] arg: byref<C1>) = arg <- C1(); true // byref<C1> -> bool
- type C2() = static member M(arg: byref<C2>) = arg <- C2(); true // byref<C2> -> bool
- type C3() = static member M(arg: outref<C3>) = arg <- C3(); true // outref<C3> -> bool
- C0.M // val it : unit -> bool * C0
- C1.M // val it : unit -> bool * C1
- C2.M // val it : C2 ref -> bool
- C3.M // val it : unit -> bool * C2
- let inline getByRef<'a when 'a: (static member M: byref<'a> -> bool)> () =
- let mutable res = Unchecked.defaultof<_>
- if (^a: (static member M: ^a byref -> bool) &res)
- then Some res
- else None
- let inline getOutRef<'a when 'a: (static member M: outref<'a> -> bool)> () =
- let mutable res = Unchecked.defaultof<_>
- if (^a: (static member M: outref<'a> -> bool) &res)
- then Some res
- else None
- getByRef<C0>() //works
- getByRef<C1>() //works
- getByRef<C2>() //works
- getByRef<C3>() //runtime error FS0193: internal error: Cannot get TypeToken for a ByRef type.
- getOutRef<C0>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
- getOutRef<C1>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
- getOutRef<C2>() //compile error FS0001: Type constraint mismatch. The type 'outref<C2>' is not compatible with type 'byref<C2>'
- getOutRef<C3>() //runtime error FS0193: internal error: Cannot get TypeToken for a ByRef type.
- //works
- match C0.M() with
- | true, value -> Some value
- | false, _ -> None
- //works
- match C1.M() with
- | true, value -> Some value
- | false, _ -> None
- //compile error FS0001: This expression was expected to have type 'C2 ref' but here has type 'unit'
- match C2.M() with
- | true, value -> Some value
- | false, _ -> None
- //works
- match C3.M() with
- | true, value -> Some value
- | false, _ -> None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement