F# - Extract parameter from Expr

久未见 提交于 2019-12-11 15:24:52

问题


My questions will no end take...

I've the function:

let hasMany (expr:Expr<'a -> seq<'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.

Short: How do I extract the seq<'b>from the Expr?


回答1:


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 @>

since id can be given the type 'b seq -> 'b seq. However, clearly <@ id @> doesn't contain a seq<'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> @>


来源:https://stackoverflow.com/questions/5759332/f-extract-parameter-from-expr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!