Thomas
Thomas

Reputation: 12087

Issue with generic taking some unrelated type, in F#

I have the following code:

let exchangeInfo, marginBrackets, accountInfo =
    async {
            let failOrExtract = function
                | Ok    d -> d
                | Error (e: ExchangeError) -> e.Describe |> tee logger.Fatal |> failwith

        let pf = processResult >> failOrExtract

        let! e = rest.FuturesUsdt.System.GetExchangeInfoAsync() |> Async.AwaitTask
        let! b = rest.FuturesUsdt.GetBracketsAsync()            |> Async.AwaitTask
        let! i = rest.FuturesUsdt.Account.GetAccountInfoAsync() |> Async.AwaitTask

        return pf e, pf b, pf i
    } |> Async.RunSynchronously

In short, it calls 3 async rest functions, then sends them through failOrExtract which is made of 2 parts: the processResult part that returns a Result<'a, ExchangeError> type, with 'a different for each of these calls, and then failOrExtract that either fails or returns the data field.

this will not compile, the compiler says:

[FS0001] The type 'BinanceFuturesUsdtExchangeInfo' does not match the type 'System.Collections.Generic.IEnumerable<BinanceFuturesSymbolBracket>'```

It looks like the failOrExtract function was generic on the first use, but not afterwards since pf e compiles, but pf b and pf i have the same error.

why am I getting this error?

Upvotes: 1

Views: 63

Answers (1)

Brian Berns
Brian Berns

Reputation: 17028

I think you're running into the F# "value restriction", although the compiler isn't being clear about it. The problem here is that pf is defined as a value, not a function, so the compiler is inferring that it has a concrete, non-generic type. In order to give pf a generic signature, try changing it to this instead:

let pf x = x |> processResult |> failOrExtract

Upvotes: 3

Related Questions