c# - 将 FSharpFun 从 C# 传递到 F#
问题描述
F#模块中函数的定义。
module ClassLibrary1.Functions
let checkThis item f =
if f item then
printfn "HIT"
else
printfn "MISS"
f# 单元测试 - 有效
[<TestMethod>]
member this.TestFunctions() =
checkThis 5 (fun x -> x > 3)
在 c# 单元测试中
using ClassLibrary1;
namespace TestProject
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestFunctions()
{
FSharpFunc<int, bool> s = x => x > 3 // error, how to declare?
Functions.checkThis(5, s);
}
}
}
错误
无法将初始化程序类型“lambda 表达式”转换为目标类型
编辑
截图帮助解答。
截图 3
解决方案
对于最新的 F# 版本 (10.2.3),请使用
var s = FuncConvert.FromFunc(new Func<int, bool>(x => x > 3));
定义了FX_NO_CONVERTER:
FSharpFunc<int, bool> s = new Converter<int, bool>(x => x > 3);
var s = FSharpFunc<int, bool>.FromConverter(x => x > 3);
var s = FuncConvert.ToFSharpFunc(new Converter<int, bool>(x => x > 3));
推荐阅读
- python - Matplotlib 动画 - 空轴
- graphql - 是否可以创建 GraphQL 子查询来创建数组作为 markdownRemark 的元素?
- python - 二维图像的地形高度预测
- sql - 如何创建检查以确保列中只有一个条目可以具有基于 SQL 中不同列的 id 的特定值?
- javascript - 自动将 Google 表格打开到最后一行第一列的脚本语言是什么?
- json - 如果它们具有相同的名称,如何在 JMeter 中循环和断言 JSON 数组对象?
- python - 如何将 Scikit Learn OneVsRestClassifier 预测方法输出转换为谷歌云 ML 的密集数组?
- c++ - 如何制作两个随机向量向量的克罗内克积?
- powershell - 如何输出 CNAME 的 FQDN?
- xamarin - Xamarin 表单视频播放器示例 - 获取用于上传的视频字节