I'm trying to work with expressions in F# (the System.Linq.Expression kind). Here is a quick example of the problem I am having:
let blah = seq {
yield Expression.New(typedefof<'T>)
yield Expression.Constant(1)
}
What I would like is for blah
to be a seq<Expression>
. However the sequence infers its type by the first yield, which is a NewExpression
. This will cause the second yield to result in a compilation failure because it is a ConstantExpression
. A working solution is to upcast all of the yields:
let blah = seq<Expression> {
yield Expression.New(typedefof<'T>) :> Expression
//or
yield upcast Expression.Constant(1)
}
But that feels unwieldy, having to upcast every single time. I thought flexible types might be a possible solution as well, but I've had trouble with that as well. seq<#Expression>
doesn't seem to work.
Is there a way I can generate a sequence of expressions without having to upcast every single one of them?