In the definition of tradeGenerated, r.AsyncDelegate will have the type Converter<Order,Trade[]> and you cast it to Converter<_,_>, which can be fine. The underscores mean that F# should infer the actual values of the type arguments used to instantiate Converter. From the next line F# can work out that the second argument must be Trade[]. However processOrder is never used in such a way that forces the first argument to be anything in particular. So from lack of evidence F# defaults the value of this type argument to be Object.

Hence the code effectively has the line

1
let processOrder = r.AsyncDelegate :?> System.Converter<Object,Trade[]>

This cast is invalid at run-time which is why you get the invalid cast class exception.

To fix the problem you must specify the value of the first type argument explicitly because it is not possible for the compiler to infer it. So the line will become

1
let processOrder = r.AsyncDelegate :?> System.Converter<Order,_>

As a rule of thumb, I would avoid using underscores in types involved with casting. Type casting is an messy OO operation and it often doesn't play well with functional-style type inference.

By on 11/8/2007 9:42 PM ()

Hi,

Thanks you! You're totally right!

Hence the code effectively has the line

1
let processOrder = r.AsyncDelegate :?> System.Converter<Object,Trade[]>

This cast is invalid at run-time which is why you get the invalid cast class exception.

So you're saying that the actual behavior of a casting depends on the types? This is really unexpected for a OCaml refugee, :(

By on 11/9/2007 1:25 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper