Error FS0001, this function is expected to be of type byref <int>

I have a simple function that wraps the general functions F # signature 'a -> 'b -> 'c optionfor a "compatible with C #" function as: 'a -> b -> byref<'c> -> bool. But for some reason, when I try to wrap such a method in a class, I get error FS0001, and I can not find the error.

Code below

open System
open System.Runtime.InteropServices

// Given a function, f: 'a -> 'b -> 'c option returns
// A new function g: 'a -> 'b -> byref<'c> -> bool
let wrapOptionF f a b (g:byref<'c>) =
    match f a b with
    | Some v ->
        do g <- v
        true
    | None -> 
        false

let tryDivide (a:int) (b:int) =
    match Math.DivRem(a,b) with
    | v, 0 -> Some v
    | _ -> None

type TryDivideWrapper() =
    static member TryDivide(a, b, [<Out>]cRef:byref<int>) : bool =
        let f = wrapOptionF tryDivide a b
        f cRef

A string of insults f cRef.

+4
source share
2 answers

This post contains a more detailed explanation, but in short you can replace your final type definition with the following:

type TryDivideWrapper() =
    static member TryDivide(a, b, [<Out>]cRef:byref<int>) : bool =
        wrapOptionF tryDivide a b &cRef

, wrapOptionF byref. byref<int> , int int ref - , (, out #). int.


: , Intellisense cRef byref<int>. , cRef, , , , int. TryDivide, a, :

let a = cRef

& , cRef f - , f, . #, TryDivideWrapper.TryDivide(a, b, out c) , . @MarkSeemann TryDivide, .

+5

, , , , :

type TryDivideWrapper() =
    static member TryDivide(a, b, [<Out>]cRef:byref<int>) : bool =
        wrapOptionF tryDivide a b &cRef

BTW, OP tryDivide tryDivide 1 0. , :

let tryDivide (a:int) (b:int) =
    if b = 0
    then None
    else Some (a / b)

FSI:

> tryDivide 1 0;;
val it : int option = None
> tryDivide 10 5;;
val it : int option = Some 2
+1

Source: https://habr.com/ru/post/1629718/


All Articles