ebb
ebb

Reputation: 9397

F# - Extract parameter from Expr

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?

Upvotes: 0

Views: 130

Answers (1)

kvb
kvb

Reputation: 55195

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

Upvotes: 3

Related Questions