F# return type coercion
Posted
by Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2010-05-08T16:40:30Z
Indexed on
2010/05/08
16:48 UTC
Read the original article
Hit count: 305
Hi,
In F# I have a function that returns System.Linq.Expression instances:
and System.Object with
member this.ToExpression() =
match this with
| :? System.Int32 -> Expression.Constant(this) :> Expression
| :? System.Boolean -> Expression.Constant(this) :> Expression
| :? Tml.Runtime.Seq as s -> s.ToExpression()
| _ -> failwith "bad expression"
If I omit the type coercions on the return values F# will infer the return type of the function to ConstantExpression. My first thought was to explicitly mark the return type as being : #Expression, but that didn't work. Is there a more elegant way of doing this that doesn't involve manually casting return types to the most generic type?
Thanks.
© Stack Overflow or respective owner