Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FSharp.Core FSharpFunc.FromConverter in netstandard20

In converting a project to .NET Standard 2.0, FSharpFunc<T, TResult>. FromConverter method from the FSharp.Core library is not available anymore. Is there a way to convert this code to a .NET Standard 2.0 FSharp.Core implementation?

async Task<Unit> RunProcess(FSharpMailboxProcessor<T> mailbox, Func<T, Task> process) { ... }

public BaseMailboxProcessor(Func<TFuncInput, Task> process, Action<Exception, TFuncInput> errorHandler = null, CancellationToken? cancellationToken = null)
{
    m_ErrorHandler = errorHandler;
    m_TokenSource = cancellationToken.HasValue ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value) :
                                                 new CancellationTokenSource();
    var cancellationOption = FSharpOption<CancellationToken>.Some(m_TokenSource.Token);
    Converter<FSharpMailboxProcessor<TMailboxInput>, FSharpAsync<Unit>> converter = (mailbox) =>
    {
        return FSharpAsync.AwaitTask<Unit>(RunProcess(mailbox, process));
    };

    m_Mailbox = new FSharpMailboxProcessor<TMailboxInput>(FSharpFunc<FSharpMailboxProcessor<TMailboxInput>, FSharpAsync<Unit>>.FromConverter(converter), cancellationOption);
    m_Mailbox.Start();
}
like image 626
Doug S. Avatar asked May 01 '26 16:05

Doug S.


2 Answers

You should be able to use FuncConvert.ToFSharpFunc, this appears to still be present in the netstandard version of FSharp.Core.

like image 113
Aaron M. Eshbach Avatar answered May 04 '26 05:05

Aaron M. Eshbach


You can trivially define your own:

let ToFSharpFunc (converter: System.Func<_, _>) = fun t -> converter.Invoke t

Also, if you need to use an FSharpFunc in C# you can simply call .Invoke on it where .Invoke on the FSharpFunc object is a Func.

like image 31
N_A Avatar answered May 04 '26 05:05

N_A