F#-从Expr提取参数 [英] F# - Extract parameter from Expr

查看:74
本文介绍了F#-从Expr提取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题永无止境...

My questions will no end take...

我具有以下功能:

let hasMany (expr:Expr<'a -> seq<'b>>)

现在我想从Expr中提取seq<'b>,因为我需要将其转换为ICollection<'b>并将其包装回新的Expr中-为什么不只是将其取为Expr首先您可能会问一个ICollection<'b>-用户首先需要简单地将seq<'b>转换为ICollection<'b>,由于我正在创建一个库,因此我试图避免这种情况除了我以外,其他人都用过,我希望它既简单又干净.

now I want to extract the seq<'b> from the Expr since I need to cast it to an ICollection<'b> and wrap it back into a new Expr - Why not just make it take an Expr that takes an ICollection<'b> in the first place you may ask - simple enough the user would need to first cast the seq<'b> to an ICollection<'b>, which I'm trying to avoid since I'm creating a library thats going to be used by others than me, and I want it to be easy and clean.

简短:如何从Expr中提取seq<'b>?

推荐答案

您的问题对我来说没有意义.根据您的类型,expr中没有seq<'b>-expr是一个包装 function 的表达式,该函数返回一个seq<'b>.例如,有了您的签名,就可以调用

Your question doesn't make sense to me. Given your types, there is no seq<'b> in expr - expr is an expression wrapping a function which returns a seq<'b>. For instance, with the signature you've got, it would be valid to call

hasMany <@ id @>

因为id可以被指定为'b seq -> 'b seq类型.但是,显然<@ id @>不包含seq<'b>

since id can be given the type 'b seq -> 'b seq. However, clearly <@ id @> doesn't contain a seq<'b>!

如果您要的是将Expr<'a -> seq<'b>>转换为Expr<'a -> ICollection<'b>>,请尝试以下操作:

If what you're asking is to convert your Expr<'a -> seq<'b>> into an Expr<'a -> ICollection<'b>>, then try this:

let hasMany (expr : Expr<'a -> 'b seq>) =
    <@ fun x -> (%expr) x :?> ICollection<'b> @>

这篇关于F#-从Expr提取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆