Skip to content

Instantly share code, notes, and snippets.

@habib-sadullaev
Last active May 22, 2020 07:08
Show Gist options
  • Save habib-sadullaev/da87549745d212fa3f826b1e5445a00b to your computer and use it in GitHub Desktop.
Save habib-sadullaev/da87549745d212fa3f826b1e5445a00b to your computer and use it in GitHub Desktop.
open System.Runtime.InteropServices
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
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: 'a byref -> 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: 'a outref -> bool)> () =
let mutable res = Unchecked.defaultof<_>
if (^a: (static member M: ^a outref -> bool) &res)
then Some res
else None
getByRef<C1>() //works
getByRef<C2>() //works
getByRef<C3>() //runtime error FS0193: internal error: Cannot get TypeToken for a ByRef type.
getOutRef<C1>() //compile error FS0001: Type constraint mismatch. The type 'outref<C1>' is not compatible with type 'byref<C1>'
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment