Attempts to pass an Action to F# code is producing the following syntax error in .net 4.6.1, VS2015...
Error CS1503 Argument 1:
cannot convert from 'System.Action<string>' to
'Microsoft.FSharp.Core.FSharpFunc<string, Microsoft.FSharp.Core.Unit>'
Run Code Online (Sandbox Code Playgroud)
The attempts are as follows...
using Microsoft.FSharp.Core;
....
Action<string> logger = Console.WriteLine;
App.perform(new Action<string>(Console.WriteLine), args);
App.perform(logger, args);
App.perform(new Action<string>(msg => Console.WriteLine(msg)), args);
App.perform((new Action<string>(msg => Console.WriteLine(msg))), args);
App.perform((new Func<string,Unit>(msg => Console.WriteLine(msg))), args);
App.perform(new Func<string,Unit>(Console.WriteLine), args);
Run Code Online (Sandbox Code Playgroud)
What is the proper way to pass System.Console.WriteLine
from C# to F#?
Microsoft.FSharp.Core.FuncConvert
提供了各种转换/适配器功能,它们中的一个可以适应Action<string>
到FSharpFunc<string, unit>
。
// Reference: FSharp.Core.dll
var writeLine = Microsoft.FSharp.Core.FuncConvert.FromAction<string>(Console.WriteLine);
App.perform (writeLine);
Run Code Online (Sandbox Code Playgroud)
问题在于,就运行时而言,Action<string>
委托和FSharpFunc<string, unit>
类是无关的,尽管我们知道它们在概念上很接近。不幸的是,C# 没有添加任何支持来帮助 F# 互操作性。