F# has both typed and untyped code quotations and I wonder what are the use cases where one would choose one over the other?
Is the distinction just convenience and untyped and typed quotations are convertible to each in all cases or are typed quotations e. g. a subset of the ones possible with untyped quotations?
Are there any examples which only work with typed, but not with untyped quotations – or the other way around?
In general, I'd recommend using typed quotations whenever you can. As usual, the types will allow you to statically enforce some correctness conditions that might otherwise cause failures at runtime. Consider:
as opposed to
Additionally, sometimes untyped quotations need additional type annotations in cases where typed quotations don't:
However, if you're building something extremely generic out of quotations, it may not be possible to use typed quotations (e.g. because the types aren't statically known). In that sense, untyped quotations give you more flexibility.
Also note that it's extremely easy to convert between typed and untyped quotations as needed (upcast an
Expr<_>
toExpr
to go from typed to untyped; useExpr.Cast
to go the other way).